Save loop variable naming problem

I want to save the answers in the loop for future use, I understand that I can use sprintf to batch store the data in the variables, but if I want to change my name with the loop, how can I do it?
ex.
for i = 1:10
k(i) = 2^i
end
k =
2 4 8 16 32 64 128 256 512 1024
I want to name the variables separately as
k1 = 2
k2 = 4
k3 = 8
....
k10 = 1024

Answers (1)

Please don't do that. It's a bad idea. Why not do it? See the FAQ:

6 Comments

I later solved my problem using a command like this
eval(['moving_time_' num2str(i) '=plot_time;']);
varname_time = sprintf('moving_time_%01d', i);
filename_time = sprintf('moving_time_%01d.mat', i);
save(filename_time, varname_time);
Because I may need to reload these data back to matlab plot in the future
ex.
plot(time1,data1)
plot(time2,data2)
...
plot(time.., data..)
Would like to ask is there any way to write the loop in this process? Or just do it one by one
Stephen23
Stephen23 on 30 Aug 2022
Edited: Stephen23 on 30 Aug 2022
"Because I may need to reload these data back to matlab plot in the future"
Much better code design would simply use exactly the same variable name in each file. Then your code would be simpler, more efficient, and much more robust.
"Would like to ask is there any way to write the loop in this process?"
Either by writing more of your complex code using dynamic variable names...
or by using exactly the same variable names in each file and writing simpler code.
"Or just do it one by one"
Computers are good at one thing: repeating simple tasks in loops. When you copy-and-paste code like that, you are just doing the computer's job for it.
You should avoid dynamic variable names.
OK So I can access the mat file with a different name
But in the mat the variables should be the same as the variables stored in each batch so that I can do calculations in the future?
You "solved" your problem in a bad way - they way we explicitly told you not to do:
eval(['moving_time_' num2str(i) '=plot_time;']);
First of all, if you have just a handful of variables, like 4 or less, it's fine to have separately named variables but just assign them immediately, by their known name, rather than in a loop and by using the hated eval().
However, if you have a large or variable number of variables, then create a 2-D array if they're all the same size, or a cell array if they (unfortunately) need to be of different lengths. For example
numVectors = 10; % or whatever it is.
moving_time = zeros(numVectors, vectorLength); % Preallocate space in advance.
for k = 1 : numVectors
thisMoving_time = plot_time; % Get the plot times for this iteration somehow.
moving_time(k, :) = thisMoving_time; % Store this time vector as row k of our 2-D array
end
% Save the 2-D array of all time vectors as a single variable in a single .mat file.
fullFileName = fullfile(pwd, 'moving_times.mat');
save(fullFileName, 'moving_time');
Great, I think this method is much better than eval Then I would like to ask how the variable I want to access today is not 1*n, but may have 68*n and then 20 years of data every year.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 30 Aug 2022

Commented:

on 31 Aug 2022

Community Treasure Hunt

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

Start Hunting!