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 commentaire

M G
M G le 11 Août 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!!!

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 12 Août 2013

5 votes

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')

11 commentaires

Carl Witthoft
Carl Witthoft le 3 Nov 2015
This should be reported as a significant bug in MATLAB. It's really sad that the "save" function can't parse a string "name.structelement" to save what we want saved.
Muhammad Haryanto
Muhammad Haryanto le 11 Août 2017
Interesting answer sir. How can I use that to save looping struck? And how can I use it for load? I want Thanks a lot sir.
Jan
Jan le 11 Août 2017
@Muhammad Haryanto: See:
S.a = 1;
S.b = 2;
save(FileName, '-struct', 'S');
Data = load(FileName)
Now Data contains the fields 'a' and 'b'.
I'm sorry to brother you again. I mean.
S.a=1;
While S.a<=5
S.a=S.a+1;
End
How can I save S.a? When I put
Save('filename.mat',-struck', 'S');
Before end, at filename just a. When I load, output is
a:[1x1 struck].
Please...
Jan
Jan le 12 Août 2017
@Muhammad: I do not understand the question. What do you want to save? Try:
save('filename1.mat', '-struct', 'S'); % Not "struck"
save('filename2.mat', 'S');
a = S.a;
save('filename3.mat', 'a');
Sara Fawal
Sara Fawal le 17 Juil 2020
Hi Jan,
My situation is a little different. I have several .mat structures and I just want to combine all of them into one GINOURMOUS .mat structure.
The mat structures caontain similar names and different types of arrays (numbers, names, date&time stamp, etc.)
How can I combine all the .mat structures irrelevant of how long the vectors are and their type.
Thank you very much for your help.
Sara Fawal
Walter Roberson
Walter Roberson le 17 Juil 2020
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 le 20 Juil 2020
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
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
Is there a way to save those mat files sequentially? I mean: create a loop saving File1.mat, File2.mat and so on. I'm trying:
for i=1:3
Ir(i) = [Ir];
end
save('Irn.mat', 'Ir');
but the Irn saves in another column the second and third variable. How to proceed correctly?
Thank you in advance.
for K = 1 : number_of_files
filename = sprintf('File%d.mat', K);
save(filename, 'Ir')
end

Connectez-vous pour commenter.

Plus de réponses (1)

the cyclist
the cyclist le 11 Août 2013

1 vote

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 commentaire

M G
M G le 11 Août 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!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by