Generate Variable Names with a Loop
563 views (last 30 days)
Show older comments
I have a simple question that I've been unable to find an answer for. I'd like to make n variable of the format:
variable_1 variable_2 variable_... variable_n
I know I should be using a for loop, but I cant figure out how to concatenate the string with the counter.
Thanks
Accepted Answer
David Sanchez
on 14 Aug 2013
N = 10; % number of variables
%method 1
for k=1:N
temp_var = strcat( 'variable_',num2str(k) );
eval(sprintf('%s = %g',temp_var,k*2));
end
% method 2, more advisable
for k=1:N
my_field = strcat('v',num2str(k));
variable.(my_field) = k*2;
end
11 Comments
Florian Flaig
on 18 Mar 2022
Edited: Florian Flaig
on 18 Mar 2022
Hello everybody,
I use variant 1, because I want the name of the structure to have the sequential number.
Now I want to store matrices in this structure.
When I use the eval(sprintf variant, it stores only the first value of the matrix under the name and not the whole matrix.
A=[3,5]
% B is a 3rd order tensor.
% => B(:,:,3) is a matrix
% => B(:,:,5) is another matrix
for k = 1:2
temp_var = strcat( 'variable_',num2str(A(i)),'.matrix' );
uTemp = B(:,:,A(i));
eval(sprintf('%s = %g',temp_var,uTemp));
end
I expect 2 matrices.
variable_3.matrix % and
variable_5.matrix
instead only the value from the top left is stored there.
What do I have to do differently?
Greetings and thank you,
Florian
Stephen23
on 18 Mar 2022
Edited: Stephen23
on 18 Mar 2022
"What do I have to do differently?"
- fix your loop iterator: the loop iterates over k, but inside the loop you refer only to i (which is undefined in your code, but presumably is defined in the workspace, thus also illustrating why experienced MATLAB users avoid scripts for reliable code).
- https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval (something else that experienced MATLAB users avoid)
More Answers (1)
Phil Kühnholz
on 24 Feb 2021
Edited: Phil Kühnholz
on 24 Feb 2021
Hi im using the 2nd method for a projekt im working on, in short i used the variables to mark images
my_field = strcat('v',num2str(k));
img = im2double(img);
variable.(my_field) = img;
I have a bunch of subplots that are filled red, if they are clicked on i want the fitting picture to be displayed.
it works if a put in the variable manually
a=click; b=str2num(cell2mat(a))
subplot(n,n,b(1));
imshow(variable.v1)
but it only gives out black blocks, nothing, or dosnt even work if i try to construct the variables name
a=click; b=str2num(cell2mat(a));
%none of these work
varistr = strcat('variable.v',num2str(b(1)));
varid = str2num(varistr1);
subplot(n,n,b(1));
imshow(varid);
%or
varid = [str2double('variable.v'),b(1)]
subplot(n,n,b(1));
imshow(varid)
%or
subplot(n,n,b(1));
imshow(variable(b(1)))
Any idea on how i can fix this?
2 Comments
Stephen23
on 24 Feb 2021
Using indexing would be much simpler.
But if you insist on this indirect approach, you probably just need to generate the appropriate fieldname:
f=sprintf('v%d',a{1});
variable.(f)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!