如何将带有结构的mat文件保存到只有数组的mat文件?

时间:2014-03-28 17:06:15

标签: arrays matlab save structure

我有一个名为C875.004-03.B401.mat的matfile,它包含一个结构C1。 C1是1X1结构,包含100个变量。我想删除结构并保存matfile,这样当我在matlab中加载它时它只是100个变量的数组。有什么想法吗?谢谢你的帮助!

2 个答案:

答案 0 :(得分:4)

或者只使用save -struct选项 -

load('C875.004-03.B401.mat')
save('C875.004-03.B401.mat','-struct','C1')

我的MATLAB版本的文档中有它 -

-struct'

Keyword to request saving the fields of a scalar structure as individual variables in the file. The structName input must appear immediately after the -struct keyword.

它也在Mathworks Help上,例如引用 -

Create a structure, s1, that contains three fields, a, b, and c.

s1.a = 12.7;
s1.b = {'abc',[4 5; 6 7]};
s1.c = 'Hello!';
Save the fields of structure s1 as individual variables in a file called newstruct.mat.

save('newstruct.mat','-struct','s1');
Check the contents of the file using the whos function.

disp('Contents of newstruct.mat:')
whos('-file','newstruct.mat')
Contents of newstruct.mat:
  Name      Size            Bytes  Class     Attributes

  a         1x1                 8  double              
  b         1x2               262  cell                
  c         1x6                12  char   

答案 1 :(得分:1)

您可以使用类matfile来帮助加载数据并将数据保存到mat文件中。以下是您案例的示例。我假设所有变量都存储为名为str的单个结构字段中的数组。如果不是这样,请纠正我。

matObj = matfile('/path/to/C875.004-03.B401.mat');
matObj.C1=matObj.C1.str;

现在,C1应该是一个数组。