考虑以下Matlab代码:
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';
pmod(2).param{1} = [1 3 5 7];
pmod(2).poly{1} = 1;
Run Code Online (Sandbox Code Playgroud)
这会创建一个struct数组.数组中的每个结构包含三个类型的字段cell.因此,我们在以下层次结构中pmod:
pmod // struct array
|
*- struct
| |
| *- cell // contains 1 or more strings
| *- cell // contains 1 or more arrays
| *- cell // contains 1 or more arrays
|
*- struct [...]
Run Code Online (Sandbox Code Playgroud)
我正在尝试用scipy.ioPython生成上述数据结构,这样它们就可以加载到Matlab中(SPM需要这种层次结构).
创建一个结构是很简单的,因为scipy.io.savemat保存任何类型的dict str …
我正在尝试使用 scipy.io 将 pandas 数据帧保存到 matlab .mat 文件。
我有以下内容:
array1 = np.array([1,2,3])
array2 = np.array(['a','b','c'])
array3 = np.array([1.01,2.02,3.03])
df = DataFrame({1:array1, 2:array2,3:array3}, index=('array1','array2','array3'))
recarray_ = df.to_records()
## Produces:
# rec.array([('array1', 1, 'a', 1.01), ('array2', 2, 'b', 2.02),
# ('array3', 3, 'c', 3.03)],
# dtype=[('index', 'O'), ('1', '<i4'), ('2', 'O'), ('3', '<f8')])
scipy.io.savemat('test_recarray_struct.mat', {'struct':df.to_records()})
Run Code Online (Sandbox Code Playgroud)
在Matlab中,我希望它生成一个包含三个数组(一个int、一个char、一个float)的结构体,但它实际上生成一个包含另外3个结构体的结构体,每个结构体包含四个变量;'index', 1, '2', 3。当尝试选择 1、'2' 或 3 时,出现错误“变量 struct(1, 1).# 不存在。”
谁能解释预期的行为以及如何最好地将 DataFrame 保存到 .mat 文件?