Skip to content Skip to sidebar Skip to footer

How Can I Create A Matlab Struct Array From Scipy.io?

Consider the following Matlab code: pmod(1).name{1} = 'regressor1'; pmod(1).param{1} = [1 2 4 5 6]; pmod(1).poly{1} = 1; pmod(2).name{1} = 'regressor2-1'; pmod(2).param{1} = [1

Solution 1:

You can use np.core.records.fromarrays to construct a record array, which is roughly equivalent to a MATLAB struct, and will be converted to a MATLAB struct by scip.io.savemat.

from numpy.core.records import fromarrays
from scipy.io import savemat

myrec = fromarrays([[1, 10], [2, 20]], names=['field1', 'field2'])
savemat('p.mat', {'myrec': myrec})

When opened in MATLAB, this gives:

>> load('p.mat')
>> myrec

myrec = 

1x2 struct array with fields:

    field1
    field2

Post a Comment for "How Can I Create A Matlab Struct Array From Scipy.io?"