Effacer les filtres
Effacer les filtres

Generate Variable Names with a Loop

450 vues (au cours des 30 derniers jours)
john
john le 14 Août 2013
Modifié(e) : Stephen23 le 18 Mar 2022
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

Réponse acceptée

David Sanchez
David Sanchez le 14 Août 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 commentaires
Florian Flaig
Florian Flaig le 18 Mar 2022
Modifié(e) : Florian Flaig le 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
Stephen23 le 18 Mar 2022
Modifié(e) : Stephen23 le 18 Mar 2022
"What do I have to do differently?"
  1. 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).
  2. https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval (something else that experienced MATLAB users avoid)

Connectez-vous pour commenter.

Plus de réponses (1)

Phil Kühnholz
Phil Kühnholz le 24 Fév 2021
Modifié(e) : Phil Kühnholz le 24 Fév 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 commentaires
Stephen23
Stephen23 le 24 Fév 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)
Phil Kühnholz
Phil Kühnholz le 24 Fév 2021
works fantastic ^^ thank u so much.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by