Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用scipy.io savemat将多个Python字典转换为MATLAB结构数组_Python_Matlab_Numpy_Scipy - Fatal编程技术网

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

使用scipy.io savemat将多个Python字典转换为MATLAB结构数组,python,matlab,numpy,scipy,Python,Matlab,Numpy,Scipy,一个简单的问题,但我似乎无法理解。我正在使用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}) 这听起来很公平,而且有效: 但是,我现在想对

一个简单的问题,但我似乎无法理解。我正在使用
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})
这听起来很公平,而且有效:

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

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

因此,我仍在为这个问题寻找适当的解决办法。如果您需要任何其他详细信息,请随时在评论中询问。谢谢你的帮助

这里有一个解决方案:

在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

@UnerableLightness有最简单的解决方案,但为了澄清
结构化数组的建议,我将给出一个示例

定义结构化数组:

In [192]: arr = np.array([(0.5,'one'),(0.6,'two'),(0.8,'three')], dtype=[('field1',float),('field2','U10')])                                                                                        
以及具有相同字段和数据的词典列表:

In [194]: dicts = [{'field1':0.5, 'field2':'one'},{'field1':0.6, 'field2':'two'},{'field1':0.8,'field2':'three'}]

In [195]: arr                                                                                          
Out[195]: 
array([(0.5, 'one'), (0.6, 'two'), (0.8, 'three')],
      dtype=[('field1', '<f8'), ('field2', '<U10')])

In [196]: dicts                                                                                        
Out[196]: 
[{'field1': 0.5, 'field2': 'one'},
 {'field1': 0.6, 'field2': 'two'},
 {'field1': 0.8, 'field2': 'three'}]
arr
是一个
struct数组
,包含两个字段:

>> arr
arr =

  1x3 struct array containing the fields:

    field1
    field2

>> arr.field1
ans =  0.50000
ans =  0.60000
ans =  0.80000
>> arr.field2
ans = one
ans = two
ans = three
dicts
是具有标量结构的单元格:

>> dicts
dicts =
{
  [1,1] =

    scalar structure containing the fields:

      field1 =  0.50000
      field2 = one

  [1,2] =

    scalar structure containing the fields:

      field1 =  0.60000
      field2 = two

  [1,3] =

    scalar structure containing the fields:

      field1 =  0.80000
      field2 = three

}
可以转换为@Unforable所示的相同结构数组:

>> [dicts{:}]
ans =

  1x3 struct array containing the fields:

    field1
    field2

>> _.field1
error: '_' undefined near line 1 column 1
>> [dicts{:}].field1
ans =  0.50000
ans =  0.60000
ans =  0.80000
>> [dicts{:}].field2
ans = one
ans = two
ans = three

另一方面,我建议(并演示)在MATLAB/Octave中创建所需的结构,然后看看
loadmat
产生了什么。或者,在python中进行一次往返可以很有启发性—一个
savemat
后跟一个
loadmat
。因此
s.dict_数组
是一个
单元格,而
[…{:}]
将其转换为
结构。
>> dicts
dicts =
{
  [1,1] =

    scalar structure containing the fields:

      field1 =  0.50000
      field2 = one

  [1,2] =

    scalar structure containing the fields:

      field1 =  0.60000
      field2 = two

  [1,3] =

    scalar structure containing the fields:

      field1 =  0.80000
      field2 = three

}
>> [dicts{:}]
ans =

  1x3 struct array containing the fields:

    field1
    field2

>> _.field1
error: '_' undefined near line 1 column 1
>> [dicts{:}].field1
ans =  0.50000
ans =  0.60000
ans =  0.80000
>> [dicts{:}].field2
ans = one
ans = two
ans = three