How can I create a Matlab struct array from scipy.io?

前端 未结 1 1719
天命终不由人
天命终不由人 2020-12-10 07:12

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条回答
  • 2020-12-10 07:38

    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
    
    0 讨论(0)
提交回复
热议问题