フィルターのクリア

Generate Variable Names with a Loop

457 ビュー (過去 30 日間)
john
john 2013 年 8 月 14 日
編集済み: Stephen23 2022 年 3 月 18 日
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
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
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
Stephen23 2022 年 3 月 18 日
編集済み: Stephen23 2022 年 3 月 18 日
"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)

サインインしてコメントする。

その他の回答 (1 件)

Phil Kühnholz
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
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)
Phil Kühnholz
Phil Kühnholz 2021 年 2 月 24 日
works fantastic ^^ thank u so much.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by