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

.mat fileaccessing field arrays in structMATLABsave workspace variablessprintfstruct

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!

Best 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.