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?

30 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 10 Oct 2014
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.
  2 Comments
Ricardo
Ricardo on 10 Oct 2014
But that means that I would have to write those commands for each file, right?
I have so many things to analyze that I was trying to automate this part...
Star Strider
Star Strider on 10 Oct 2014
Edited: Star Strider on 10 Oct 2014
If your file names are of the sort I described, you can certainly load them in a loop and save the ‘F’ array at the same time:
for k1 = 1:nfiles
M = load(sprintf('file%d.mat',k1));
F{k1} = M.F;
end
I didn’t test that particular loop in the interests of time, but it should work.
The cell array will take ‘F’ arrays of different sizes without throwing an error.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 10 Oct 2014
When loading back in, read the mat file into a structure and extract the F from that one mat file into the p'th element of an overall F array that you build up.
for p = 1 : whatever
thisFileName = sprintf('%d.mat', p); % or whatever......
storedStructure = load(thisFileName);
% Extract out this F and save it in the overall F array
F(p) = storedStructure.F;
end
  4 Comments
Image Analyst
Image Analyst on 10 Oct 2014
Yes, cell arrays are more complicated and you should avoid them when you can . For example if F is a 2D array, you can stick them all into different planes/slices of a 3D array - a regular numerical one like double, not cell. This FAQ entry may explain cells better in a more intuitive way: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

Sign in to comment.

Categories

Find more on Licensing on Cloud Platforms in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!