Generate Variable Names with a Loop
조회 수: 563 (최근 30일)
이전 댓글 표시
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
채택된 답변
David Sanchez
2013년 8월 14일
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
Florian Flaig
2022년 3월 18일
편집: Florian Flaig
2022년 3월 18일
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
2022년 3월 18일
편집: Stephen23
2022년 3월 18일
"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)
추가 답변 (1개)
Phil Kühnholz
2021년 2월 24일
편집: Phil Kühnholz
2021년 2월 24일
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
Stephen23
2021년 2월 24일
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)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!