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

5 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
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

Community Treasure Hunt

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

Start Hunting!