Saving workspace variables with different .mat file name using for loop
Ältere Kommentare anzeigen
Hello all,
I just wanted to save workspace variables as a .mat file whose set of variables are extracted from a struct that consists of time and data fields in a timeseries struct and therfore the different variables should be named differently,
i have tried the ff
for i=1:N
temp=x{i,1}.data
save temp.mat temp
end
unfortunatly the above code saves only the last iteration (Nth iteration),any help appreciated to access all the N elements,and eventually creat N math files,
Thank you in Advance!
8 Kommentare
Rik
am 7 Jan. 2019
If you want to give the mat files each its own name, you can use sprintf to generate a name from a pattern.
Stephen23
am 7 Jan. 2019
"...and therfore the diffrent variables should be named diffrently,"
That would not be a good approach. Read this to know why:
It is really much easier to process a sequence of .mat files if the variable names are exactly the same for each .mat file, and only the filename changes.
Zeab
am 9 Jan. 2019
@Zeab: look at your code:
for i=1:10
temp=x{i,1}.data;
fname=sprintf('myfield%d.mat',i);
save(filename)
end
On the third line you define fname... which you never use. And on the fourth line you try to use filename, which you never define. Have another look at my answer: did I use the same variable fnm on both lines three and four? Did you?
Zeab
am 9 Jan. 2019
Stephen23
am 9 Jan. 2019
"...it is desired to save the 1D data field only with 10 diffrent .mat file name."
You can use my answer. Just extract the required data into the variable temp and my code will generate a new filename on each loop iteration.
Zeab
am 9 Jan. 2019
Zeab
am 9 Jan. 2019
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!