Generate Variable Names with a Loop

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

john
john le 14 Août 2013
Thanks, num2str works perfectly.
While the first method using eval is a bad idea, the 2nd can be abbreviated a little bit:
my_field = sprintf('v%d', k);
Forcing meta-data into variable names is slow and complex.
Indexing is much simpler and much more efficient:
% method 3, even more advisable:
C = cell(1,N);
for k = 1:N
C{k} = k*2;
end
Dav
Dav le 8 Jan 2021
Thanks for this method Stephen. Short, simple, and works perfectly.
Soumya Mishra
Soumya Mishra le 21 Mar 2021
Hi. I tried using this in my program, I was expecting a 1X100 cell array and each cell value must show 23x20 double. But unfortunately all my cells show blank []
clc
clear all
close all
% x = [];
% y = [];
t0 = readtable('master_mfcc_positive.xlsx');
t1 = readtable('mfcc_master_negative.xlsx');
S0 = table2array(t0);
S1 = table2array(t1);
% S = vertcat (S0, S1);
m1=12;
m2=11;
p = 100;
for i = 1:m1:1200
for j = 1:m2:1100
for k = 1:p
C = cell(1,p);
C{k} = vertcat(S0(i:k*m1,:), S1(j:k*m2,:));
% y{p} = x{p}((i:p*m1 + j:p*m2),19);
end
end
end
for k = 1:p
C = cell(1,p);
C{k} = vertcat(S0(i:k*m1,:), S1(j:k*m2,:));
% y{p} = x{p}((i:p*m1 + j:p*m2),19);
end
Every iteration of k, you throw away whatever was in C and replace it with a cell array containing empty entries. Then you assign something into one of those entries.
By the time you reach the end of the k loop, only the last of the entries will have anything stored in it.
The last iteration of i will be i = 1189 . The last iteration of j will be j = 1090. So you will extract S0(1189:100*12,:)) and S1(1090:100*11, :) and concatenate them together. That is 12 rows and 11 rows, total of 23 rows, which checks out with your requirement for 23 rows. If only you were not
C = cell(1,p)
each iteration of k. And if only you used C after the end of the for k loop so that you did not effectively throw away the work for all of the i and j iterations except the last.
Soumya Mishra
Soumya Mishra le 21 Mar 2021
Modifié(e) : Soumya Mishra le 21 Mar 2021
Thanks a lot Walter. I understood your point. But please help me with getting something like C{1}, C{2}....C{100}
I intend to go for a classification problem and i have to use 100 pairs of C(for X) and Y for creating SVM Model.
My aim was to use a loop for getting :
% x2 = vertcat(S1, S10);
% y2 = x2(:,20);
% x3 = vertcat(S2, S11);
% y3 = x3(:,20);
% x4 = vertcat(S3, S12);
% y4 = x4(:,20);
% x5 = vertcat(S4, S13);
% y5 = x5(:,20);
% x6 = vertcat(S5, S14);
% y6 = x6(:,20);
% x7 = vertcat(S6, S15);
% y7 = x7(:,20);
% x8 = vertcat(S7, S16);
% y8 = x8(:,20);
% x9 = vertcat(S8, S17);
% y9 = x9(:,20);
% x10 = vertcat(S8a, S17a);
% y10 = x10(:,20);
% x11 = vertcat(S8b, S17b);
% y11 = x11(:,20);
%
% x12 = vertcat(S8c, S17c);
% y12 = x12(:,20);
%
% x13 = vertcat(S8d, S17d);
% y13 = x13(:,20);
%
% x14 = vertcat(S8e, S17e);
% y14 = x14(:,20);
%
% x15 = vertcat(S8f, S17f);
% y15 = x15(:,20);
%
% % score = LDA(x7, y7)
% % [index,featureScore] = feature_rank(x7,y7,15,'Fisher_Score')
% SVMModel1 = fitcsvm(x1,y1,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel2 = fitcsvm(x2,y2,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel3 = fitcsvm(x3,y3,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel4 = fitcsvm(x4,y4,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel5 = fitcsvm(x5,y5,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel6 = fitcsvm(x6,y6,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel7 = fitcsvm(x7,y7,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel8 = fitcsvm(x8,y8,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel9 = fitcsvm(x9,y9,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel10 = fitcsvm(x10,y10,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
% SVMModel11 = fitcsvm(x11,y11,'Standardize',true,'KernelFunction','RBF',...
% 'KernelScale','auto');
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

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

Community Treasure Hunt

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

Start Hunting!

Translated by