How can I create dynamic variables A1, A2,...,A10 in a loop and save them?

11 views (last 30 days)
Hello community! I have seen that this question is timeless, with several modifications, but most cases are general. So the script that puzzles me has the following setting:
  1. Suppose that you are using some functions and you want to loop their arguments (arg) to get different results.
  2. In each iteration you want to save a variable and name it using the current arguments
  3. Finally, all the variables created should be saved in a new .m file.
Schematically,
for arg1 = 0.1:+0.1:1.5
for arg2 = 0.1:+0.1:1.5
for arg3 = 0.1:+0.1:1.5
for arg4 = 0.1:+0.1:1.5
Object1 = function(arg1, arg2)
Object1.Specification = function(arg3, arg4)
Variable_arg2_arg4 = function(Object1) % *
save(fullfile(path, name), all variables created) % **
end
end
end
end
The steps that trouble me are those highlighted with [*] and [**].
I know that a vectorized method for saving the variables created in each iteration is more clear-cut, but in this case I cannot think of a better solution other than creating them dynamically. However, any suggestion is greatly appreciated!
  17 Comments
MikCh
MikCh on 16 May 2017
Edited: MikCh on 16 May 2017
@ Stephen Cobeldick: Thanks for the super informative answer!!
Let me modify a bit the code that you have provided and comment on the procedure:
vSpeed = (0.1:+0.1:1.5);
vHeight = (0.1:+0.1:1.5);
dataC = cell(numel(vSpeed),numel(vHeight));
for kSpeed = 1:numel(vSpeed)
for kHeight = 1:numel(vHeight)
obj = function(vSpeed(kSpeed),vHeight(kHeight));
filename = sprintf('File_%.2f_%.2f.mat', vSpeed, vHeight);
fpath = fullfile(dpath,filename);
save(fpath,'obj','kSpeed','kHeight');
end
end
Steps of Stephen's approach:
  1. vSpeed & vHeight: In the first two lines you translate the iterations into variables so as to avoid over-looping
  2. dataC: you create a cell that stores the name of the variables? (I'm not sure about that since you do not use it inside the loop)
  3. obj: here you run the function using an indexing method for the arguments
  4. filename: you print a mat file for each iteration. The name of the mat file includes the arguments of the iteration.
  5. fpath: you create the path where the specific mat file will be saved
  6. save(...): you save the items 'obj','kSpeed','kHeight' into the file
When I do run this code, the following points arise:
  • I think that storing 'vSpeed' and 'vHeight', as being the actual parameters of the equations, might be better instead of storing 'kSpeed' and 'kHeight'.
  • How should I use the dataC cell?
  • The way that filename appears in the loop, gives back a char object (size 1x270) that goes like this: "file_0.10_0.20.matfile_0.30_0.40.matfile_0.50_0.60.matfile_0.70_0.80.matfile_0.90_1.00.matfile_1.10_1.20.matfile_1.30_1.40.matfile_1.50_0.10.matfile_0.20_0.30.matfile_0.40_0.50.matfile_0.60_0.70.matfile_0.80_0.90.matfile_1.00_1.10.matfile_1.20_1.30.matfile_1.40_1.50.mat" Hence, I am doing something totally wrong!
  • what is "dpath" inside the fullfile?
  • Your suggestion is to store each variable in a separate mat file?
Thanks again for your feedback!
p.s. sorry for the confusion about .m and mat files, I was referring to mat files
Stephen23
Stephen23 on 16 May 2017
Edited: Stephen23 on 16 May 2017
  1. "In the first two lines you translate the iterations into variables so as to avoid over-looping"... errr, I have no idea what that means. I created the vectors vSpeed and vHeight simply because then the loop variables kSpeed and kHeight can be indices (i.e. integers from one). Iterating over indices is often more useful than iterating over values (for example here it makes saving the values in vSpeed and vHeight easier, because they are in simple vectors which are easy to handle).
  2. That line does nothing useful here. I simply forgot to remove it. In any case, it does not store the names of any variables, it simply creates a cell array of empty cells, which is then never used. You ca get rid of this line. (Lines like this are useful when calculating values in a loop, and then putting them into an array). The reason I used two variables was because I did not want to make the example complicated. You can use four, or five, or however many vectors of values that makes you happy.
  3. Yes.
  4. sprintf creates a 1xN char array. The documentation calls it "format data into string". Using the term "print" is a bit misleading, as it does not send any data to the stdout or the like.
  5. Yes.
  6. Yes.
And your points:
  • You will could store both, or just vSpeed and vHeight. Whatever suits your needs. Note that using indices might be simpler in the filenames.
  • It is not used unless you want to join some data into one array. Otherwise ignore Cdata.
  • The absolute or relative path of the location where the files are to be saved. Do not cd to that directory, and there is no point in cluttering up your current directory with files like that, when using absolute or relative paths makes it trivial to read/write files anywhere. Read the help for fullfile.
  • "Your suggestion is to store each variable in a separate mat file?" No, that was just what you showed in your question. I have no idea about your data: how many samples there are, if they are contiguous, what format they is stored in, if it takes 100 kB or 100 TB of storage, how you want to access them, etc etc. Without concrete examples or specifications of your data I am not going to waste my time guessing how it should be stored. Maybe one variable per file is efficient, or then again maybe not. For small-medium amount of data, most likely not. It might be more efficient to write the data to file after the loops (and then you will need to use Cdata or something similar, like an ND array). As I have no idea how much data you have, whether it is numeric or string, or something else entirely, I will not make any guesses.

Sign in to comment.

Answers (2)

Rik
Rik on 11 May 2017
You could use eval to do what you are asking, BUT YOU SHOULD NOT DO THAT.
Instead you could use the values of the arguments to generate a file name that does this. For that sprintf is all yours to play with. Type doc sprintf to find out everything you can do with it.
You should try to rework this, so you can remove some of the inefficiency here. One of the more easier ways to this is generating a cell matrix, so you can use cellfun to run every combination you need.

Jan
Jan on 11 May 2017

Categories

Find more on Variables 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!