使用 scipy.io savemat 将多个 Python 字典转换为 MATLAB 结构体数组

一个简单的问题,但我似乎无法理解。我正在使用该scipy.io库将 Python 字典保存为 Matlab 结构。现在,该库的文档scipy.io向我们展示了如何将单个 Python 字典转换为单个 Matlab 结构:

>>> a_dict = {'field1': 0.5, 'field2': 'a string'}
>>> sio.savemat('saved_struct.mat', {'a_dict': a_dict})

这听起来很公平,并且有效:

http://img2.mukewang.com/6360e11a0001651102640226.jpg

但是,我现在想对多个 Python 字典执行相同的操作。我希望将它们转换为 Matlab 结构,其中列名等于所有字典的键(显然都是相同的键名),并且我希望每一行代表其中一个键的值字典。如果我没看错,这称为 1 x K 结构,有 10 个字段,其中 K 是我要映射的行数(Python 字典)。字段示例如下所示:

http://img2.mukewang.com/6360e1240001cd2406570117.jpg

尽管我自己完全不知道正确的 Matlab 术语,但评论中的一个好人告诉我这应该被称为结构数组。我尝试简单地创建一个 Python 字典的 numpy 数组,将其放在a_dict上面代码示例的键值对中并保存,但没有成功。这样做会导致所有不同结构的列表,而不是一个大结构,其中行表示每个单独结构的值。

因此,我仍在为这个问题寻找合适的解决方案。如果您需要任何其他详细信息,请随时在评论中提问。感谢您的帮助!


杨魅力
浏览 161回答 2
2回答

慕的地8271018

这是一个解决方案:在 Python 中:>>> a_dict = {'field1': 0.5, 'field2': 'a string'}>>> b_dict = {'field1': 1, 'field2': 'another string'}>>> sio.savemat('saved_struct.mat', {'dict_array':[a_dict,b_dict]})在 MATLAB 中:s = load('saved_struct.mat');struct_array = [s.dict_array{:}];您将根据需要在 MATLAB 中得到一个结构体数组。struct_array =   1×2 struct array with fields:    field1    field2

阿波罗的战车

为了澄清structured array建议,我将举一个例子。定义一个结构化数组:In [192]: arr = np.array([(0.5,'one'),(0.6,'two'),(0.8,'three')], dtype=[('field1',float),('field2','U10')])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;以及具有相同字段和数据的字典列表:In [194]: dicts = [{'field1':0.5, 'field2':'one'},{'field1':0.6, 'field2':'two'},{'field1':0.8,'field2':'three'}]In [195]: arr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[195]:&nbsp;array([(0.5, 'one'), (0.6, 'two'), (0.8, 'three')],&nbsp; &nbsp; &nbsp; dtype=[('field1', '<f8'), ('field2', '<U10')])In [196]: dicts&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[196]:&nbsp;[{'field1': 0.5, 'field2': 'one'},&nbsp;{'field1': 0.6, 'field2': 'two'},&nbsp;{'field1': 0.8, 'field2': 'three'}]保存和加载:In [197]: io.savemat('ones.mat', {'arr':arr, 'dicts':dicts})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [198]: io.loadmat('ones.mat')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[198]:&nbsp;{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Fri May&nbsp; 1 09:06:19 2020',&nbsp;'__version__': '1.0',&nbsp;'__globals__': [],&nbsp;'arr': array([[(array([[0.5]]), array(['one'], dtype='<U3')),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(array([[0.6]]), array(['two'], dtype='<U3')),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(array([[0.8]]), array(['three'], dtype='<U5'))]],&nbsp; &nbsp; &nbsp; &nbsp;dtype=[('field1', 'O'), ('field2', 'O')]),&nbsp;'dicts': array([[array([[(array([[0.5]]), array(['one'], dtype='<U3'))]],&nbsp; &nbsp; &nbsp; &nbsp;dtype=[('field1', 'O'), ('field2', 'O')]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array([[(array([[0.6]]), array(['two'], dtype='<U3'))]],&nbsp; &nbsp; &nbsp; &nbsp;dtype=[('field1', 'O'), ('field2', 'O')]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array([[(array([[0.8]]), array(['three'], dtype='<U5'))]],&nbsp; &nbsp; &nbsp; &nbsp;dtype=[('field1', 'O'), ('field2', 'O')])]], dtype=object)}savemat创建了一些对象 dtype 数组(和字段)和 2d MATLAB 类数组。在 Octave 会话中:>> load ones.mat这arr是一个struct array有2个字段:>> arrarr =&nbsp; 1x3 struct array containing the fields:&nbsp; &nbsp; field1&nbsp; &nbsp; field2>> arr.field1ans =&nbsp; 0.50000ans =&nbsp; 0.60000ans =&nbsp; 0.80000>> arr.field2ans = oneans = twoans = threedicts是具有标量结构的单元:>> dictsdicts ={&nbsp; [1,1] =&nbsp; &nbsp; scalar structure containing the fields:&nbsp; &nbsp; &nbsp; field1 =&nbsp; 0.50000&nbsp; &nbsp; &nbsp; field2 = one&nbsp; [1,2] =&nbsp; &nbsp; scalar structure containing the fields:&nbsp; &nbsp; &nbsp; field1 =&nbsp; 0.60000&nbsp; &nbsp; &nbsp; field2 = two&nbsp; [1,3] =&nbsp; &nbsp; scalar structure containing the fields:&nbsp; &nbsp; &nbsp; field1 =&nbsp; 0.80000&nbsp; &nbsp; &nbsp; field2 = three}可以转换为与@Unbearable 显示的相同的结构数组:>> [dicts{:}]ans =&nbsp; 1x3 struct array containing the fields:&nbsp; &nbsp; field1&nbsp; &nbsp; field2>> _.field1error: '_' undefined near line 1 column 1>> [dicts{:}].field1ans =&nbsp; 0.50000ans =&nbsp; 0.60000ans =&nbsp; 0.80000>> [dicts{:}].field2ans = oneans = twoans = three
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python