python - 将结构体元胞数组从 Python 传递到 MATLAB

标签 python matlab structure cell-array

感谢 scipy.io将结构或元胞数组等对象从 Python 快速轻松地传递到 MATLAB。

MATLAB 结构:

<小时/>
  • 在 Python 中:

import scipy.io as sio

titi={'oui': 'Y', 'non': 'N', 'AgeDuCapitaine': 53}
sio.savemat('titi.mat', {'titi': titi})

  • 在 Matlab 中:

load('titi')
titi

    titi = 

    AgeDuCapitaine: 53
               oui: 'Y'
               non: 'N'

titi.AgeDuCapitaine

    ans =

                   53

MATLAB 元胞数组:

<小时/>
  • 在 Python 中:

import scipy.io as sio
import numpy as np

tutu=np.zeros((3,), dtype=np.object)
tutu[0]=1
tutu[1]='omg'
tutu[2]=np.zeros((2,), dtype=np.object)
tutu[2][0]='vrai'
tutu[2][1]=2
sio.savemat('tutu.mat', {'tutu': tutu})

  • 在 Matlab 中:

load('tutu')
tutu

    tutu = 

    [1]    'omg'    {1x2 cell}

tutu{1}

    ans =

                1

tutu{3}{1}

    ans =

                vrai

但是,假设我们想要传递一个与混合 MATLAB 结构体元胞数组相对应的对象,例如最终的 MATLAB 对象,例如:
toto{1}.weapon{2}.Name='fleurs' ...

MATLAB 结构元胞数组:

<小时/>

在 python 中(测试,尚未令人信服!):

import scipy.io as sio
import numpy as np

toto = np.zeros((2,), dtype=np.object)
toto[0] = {}
toto[1] = {}
toto[0]['weapon'] = np.zeros((2,), dtype=np.object)
toto[0]['weapon'][0] = {}
toto[0]['weapon'][1] = {}
toto[0]['weapon'][1]['Name'] = 'fleurs'
toto

   array([{'weapon': array([{}, {'Name': 'fleurs'}], dtype=object)}, {}], dtype=object)

sio.savemat('toto.mat', {'toto':toto})

   Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio.py", line 207, in savemat
         MW.put_variables(mdict)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 876, in put_variables
         self._matrix_writer.write_top(var, asbytes(name), is_global)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 626, in write_top
         self.write(arr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 655, in write
         self.write_cells(narr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 759, in write_cells
         self.write(el)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 653, in write
         self.write_struct(narr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 764, in write_struct
         self._write_items(arr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 782, in _write_items
         self.write(el[f])
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 655, in write
         self.write_cells(narr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 759, in write_cells
         self.write(el)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 647, in write
         % (arr, type(arr)))
   TypeError: Could not convert {} (type <type 'dict'>) to array

那么,是否可以在 Python 中创建结构体元胞数组并将其传递给 MATLAB?
我是不是搞错了?

最佳答案

哎呀我刚刚明白为什么会引发 TypeError 异常...... 事实上,这是将结构体元胞数组从 Python 传递到 MATLAB 的正确方法,但是...... scipy.io.savemat 似乎不允许空字典。 事实上,如果我完成所有词典,一切都工作正常!:

在Python中:

>>> import scipy.io as sio   
>>> import numpy as np

>>> toto = np.zeros((2,), dtype=np.object)
>>> toto[0]={}
>>> toto[1]={}
>>> toto[1]['Site']=['Tataouine']
>>> toto[0]['weapon'] = np.zeros((2,), dtype=np.object)
>>> toto[0]['weapon'][0]={}
>>> toto[0]['weapon'][1]={}
>>> toto[0]['weapon'][1]['Name']='fleurs'
>>> toto[0]['weapon'][0]['Name']='bonbons'
>>> toto
array([ {'weapon': array([{'Name': 'bonbons'}, {'Name': 'fleurs'}], dtype=object)},
       {'Site': ['Tataouine']}], dtype=object)
>>> sio.savemat('toto.mat', {'toto':toto})

在 MATLAB 中:

>> load('toto')
>> toto

toto = 

    [1x1 struct]    [1x1 struct]

>> toto{1}.weapon{2}.Name

ans =

fleurs

希望这能帮助您不要在这个愚蠢的问题上浪费时间!!

关于python - 将结构体元胞数组从 Python 传递到 MATLAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42784943/

相关文章:

mysql - 在数据库中存储标签的最佳实践?

python - 如何pickle ssl.SSLContext 对象

Python 获取电子邮件

python - 重新着色图像

c - 使用 openMP 和 gcc 编译时 MEX 文件无效

c++ - OpenCV vs MATLAB(图像处理的形状测量功能)

python - 如何在 matplotlib 的折线图上绘制最大值点?

matlab - 在 MATLAB 中关闭所有方差分析窗口

c - 结构,malloc ..我再困惑不过了

c - 动态分配数组结构的数组