@@ -31,7 +31,7 @@ module Foreign.Matlab.Array (
31
31
-- | array list access
32
32
mxArrayGetList , mxArraySetList ,
33
33
mxArrayGetAll , mxArraySetAll ,
34
- fromListIO ,
34
+ fromListIO , cellFromListsIO ,
35
35
36
36
-- * Struct access
37
37
-- | Structs in Matlab are always arrays, and so can be accessed using most array accessors.
@@ -50,6 +50,7 @@ module Foreign.Matlab.Array (
50
50
) where
51
51
52
52
import Control.Monad
53
+ import Data.Foldable (toList )
53
54
import Foreign
54
55
import Foreign.C.String
55
56
import Foreign.C.Types
@@ -128,10 +129,21 @@ freeMXArray :: MXArray a -> MIO ()
128
129
freeMXArray a = withMXArray a mxDestroyArray
129
130
130
131
-- | Create and populate an MXArray in one go.
131
- fromListIO :: MXArrayComponent a => [ a ] -> MIO (MXArray a )
132
+ fromListIO :: ( Foldable t , MXArrayComponent a ) => t a -> MIO (MXArray a )
132
133
fromListIO xs = do
133
134
arr <- createMXArray [length xs]
134
- mxArraySetAll arr xs
135
+ mxArraySetAll arr xsList
136
+ pure arr
137
+ where
138
+ xsList = toList xs
139
+
140
+ -- | Like fromListIO but wraps elements in a cell. Most useful for converting a list of strings
141
+ -- | to a MATLAB cell array of strings.
142
+ cellFromListsIO :: (Traversable s , Foldable t , MXArrayComponent a ) => s (t a ) -> MIO (MXArray MCell )
143
+ cellFromListsIO xss = do
144
+ listOfStructArrays <- sequence $ fromListIO <$> xss
145
+ arr <- createMXArray [length xss]
146
+ mxArraySetAll arr (toList $ (MCell . anyMXArray) <$> listOfStructArrays)
135
147
pure arr
136
148
137
149
-- | The class of standardly typeable array elements
0 commit comments