MATLAB Answers

Save Struct as .mat file!!!

1,359 views (last 30 days)
M G
M G on 11 Aug 2013
Commented: Walter Roberson on 20 Jul 2020 at 20:25
Hello all,
I have a problem in saving Struct format in .mat file.
If I have "a" as 1x5 struct
So, in command window it looks like this:
a =
ans =
246
ans =
444
ans =
630
ans =
810
ans =
984
Any way that I can save it as a .mat file of 1x5?
Thanks so much for your help :)

  1 Comment

M G
M G on 11 Aug 2013
Here is the file:
https://dl.dropboxusercontent.com/u/19202474/matFile.zip
I want to save
a.latency
which consists of 947 value in one mat file. I do like:
save filename.mat a.latency
... but doesn't work!!!

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Aug 2013
Of course this does not work, because "a.latency" is not a name of a variable. You need:
save('filename.mat', 'a')
The functional form is less confusing for the save command.
If you want to save the fields "latency" only:
latency = {a.latency};
save('filename.mat', 'latency')

  9 Comments

Show 6 older comments
Walter Roberson
Walter Roberson on 17 Jul 2020 at 9:36
If there are two .mat files that have exactly the same variable name, how do you want that to appear on output? Do you want the contents merged together into a single variable? Do you want new unique variable names generated?
Sara Fawal
Sara Fawal on 20 Jul 2020 at 16:07
Hello Walter,
The .mat embeded structures are all unique and do not have the same names nor the same variables.
I just want to combine them all. I have many .mat embeded structures and I want them all in one GINOURMOUS .mat embeded structure.
Thank you again for your help.
Sara Fawal
Walter Roberson
Walter Roberson on 20 Jul 2020 at 20:25
inputdir = 'appropriate directory path';
outputfile = 'appropriate file name'; %do not put it into inputdir !
dinfo = dir( fullfile(inputdir, '*.mat') );
filenames = fullfile( inputdir, {dinfo.name} );
for K = 1 : length(filenames)
thisfile = load(filenames{K});
save(outfile, '-struct', 'thisfile', '-append');
end

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 11 Aug 2013
What do you mean by "a .mat file of 1x5"?
>> save filename.mat a
will save the structure, exactly as it is in the workspace.
>> load filename.mat a
will load it back into the workspace.

  1 Comment

M G
M G on 11 Aug 2013
https://dl.dropboxusercontent.com/u/19202474/matFile.zip
I uploaded the .mat file in here. I want to save a.latency which consists of 947 value in one mat file. I do like:
save filename.mat a.latency
but doesn't work!!

Sign in to comment.