How to save a list of python dictionaries as an array of matlab structured arrays?

前端 未结 1 1685
耶瑟儿~
耶瑟儿~ 2021-01-15 18:23

I am trying to create a file to be read in a matlab enviroment. The structure in matlab looks like this

trx(1) = 
          x: [1×1500 double]
          y: [         


        
相关标签:
1条回答
  • 2021-01-15 18:51

    In Octave

    M =
    
      scalar structure containing the fields:
    
        x =
    
           1   2   3   4
    
        y =
    
           5   6   7   8
    
        one =  1
        two =
    
           1   2
    
    >> save -7 struct.mat M
    

    In Ipython:

    In [450]: dat = io.loadmat('struct.mat')
    In [451]: dat
    Out[451]: 
    {'__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.2.2, 2019-02-08 18:49:49 UTC',
     '__version__': '1.0',
     '__globals__': [],
     'M': array([[(array([[1., 2., 3., 4.]]), array([[5., 6., 7., 8.]]), array([[1.]]), array([[1., 2.]]))]],
           dtype=[('x', 'O'), ('y', 'O'), ('one', 'O'), ('two', 'O')])}
    

    Here M is (1,1) structured array, with all fields being object dtype. That way they can each have their own shape. The scalar is a (1,1) matrix.

    0 讨论(0)
提交回复
热议问题