MATLAB: How to save the same variable in an iteration with different names so that when I load them they don’t overwrite the previous one

iterationloadMATLABsave

Hi guys,
My knowledge of MatLab (and coding in general) is extremely limited so I would appreciate if someone could help me with this.
So, I have an iteration in MatLab and at the end of each step a variable 'F' is calculated. Ideally, I would like to save a new file (.mat) each time to the 'Current Folder' as F1, F2, F3, etc.
Right now what I have is this:
for p=1:pillar;
directory=[pwd '\pillar_' num2str(p)]
cd(directory)
(more stuff inbetween)
F =d*(1/0.93)*3.25;
save (num2str(p), 'F')
end
In each iteration this code is saving the variable 'F' as 1, 2, 3, etc, in the 'Current Folder', which would be okay.
The problem is that I have to load all the files into the workspace when the iteration is complete. When I load the file '1.mat' it appears into the workspace as 'F'; then I load the file '2.mat' and it overwrites the previous file because it also appears as 'F'; etc.
So I need a way to save the variable 'F' in each iteration that will allow me at the end to just load all the files without overwriting the previous one.
Thanks, Ricardo

Best Answer

The solution is not with the save — create ‘F’ in each iteration and save it to a different .mat file — but with load. Use load with an output argument, then create variables in your workspace as necessary.
For example:
M1 = load('file1.mat');
F1 = M1.F;
M2 = load('file2.mat');
F2 = M2.F;
although I would save the ‘F’ values in a numeric or cell array (as ‘F{1}’, ‘F{2}’ etc., rather than as separate variables if possible. That makes programming using them considerably easier.