Saving workspace variables with different .mat file name using for loop
Show older comments
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 Comments
Rik
on 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
on 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
on 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
on 9 Jan 2019
Stephen23
on 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
on 9 Jan 2019
Zeab
on 9 Jan 2019
Accepted Answer
More Answers (0)
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!