Saving workspace variables with different .mat file name using for loop

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

If you want to give the mat files each its own name, you can use sprintf to generate a name from a pattern.
"...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.
can you explain it more?how could i generate 10 . mat files extracted from a timeseries data of x with data and time fields.I am interested on the data field.I have tried the follwing:
>> for i=1:10
temp=x{i,1}.data;
fname=sprintf('myfield%d.mat',i);
save(filename)
end
but still something wrong,can you explain it more how to fix the bug
@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?
Thank you for your help,i have tried the above section of code ,by doing so it is possible to allocate 10 diffrent .mat files but can't get find the values as i desired.Let me make things clear:
given the attached timeseries data named Vb1nicw.mat,it is desired to save the 1D data field only with 10 diffrent .mat file name..
"...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.
Have you seen that the above section of code can only generate a new file name for every loop iteration but assigning the data i.e the variable temp to the .mat file fanme can't succesufly implemented.
I wonder if you could try the attached ,mat file (Vb1nicw.mat) to check the above section of code.
Thank you in advance!

Sign in to comment.

 Accepted Answer

My guess at what you want:
filestruct = load('Vb1nicw.mat');
fn = fieldnames(filestruct);
for K = 1 : length(fn)
thisfield = fn{K};
tempvar = struct(thisfield, filestruct.(thisfield));
outfile = [thisfield '.mat'];
save(outfile, '-struct', 'tempvar');
end
This will take each variable in Vb1nicw.mat and will save it out to a separate .mat file that is named after the variable, and the variable name used inside the .mat file will be the same as the name of the variable.

3 Comments

I have tried your suggestion but still something wrong,
for the .mat file i have attached earlier,i have programmed the follwing using for loop unfortunately it overwrites the .mat file and display only the last iteration
Here it is,
>> for i=1:length(Vb1nicw)
tempvar=Vb1nicw{i,1}.data;
save tempvar.mat tempvar
end
for K=1:length(Vb1nicw)
fn = sprintf('Vb1nicw_%d', K);
tempvar = struct(fn, Vb1nicw{K});
fnm = [fn '.mat'];
save(fnm, '-struct', 'tempvar');
end
This will save to files Vb1nicw_1.mat through Vb1nicw_10.mat, using a variable that has the same name as the file name.
You have not been clear as to what filenames you want to use or what variable names you want to use within those files.
works perfectly,you solved my trouble!
Thanks

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Asked:

on 7 Jan 2019

Edited:

on 9 Jan 2019

Community Treasure Hunt

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

Start Hunting!