user3353139
user3353139

Reputation: 11

How to save a matfile with structure to a matfile with just an array?

I have a matfile named C875.004-03.B401.mat that contains a structure C1. C1 is a 1X1 structure that contains 100 variables. I want to remove the structure and save the matfile such that it is only an array of the 100 variables when I load it in matlab. Any thoughts? Thank you for the help!

Upvotes: 1

Views: 385

Answers (2)

freude
freude

Reputation: 3832

You can use class matfile which helps with loading and saving data to mat-files. Below is an example for your case. I assume that all variables are stored as an array in the single structure's field named str. Correct me if it is not the case.

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

Now, C1 should be an array.

Upvotes: 1

Divakar
Divakar

Reputation: 221684

Or just use save with it's -struct option -

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

It's there in the documentation from my MATLAB version -

-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.

It's there on the Mathworks Help too with example, as quoted here -

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   

Upvotes: 4

Related Questions