how to work with variables with different names in a loop?
82 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have a bunch of data taken in the lab which has names such as A1,A2... AN ... ETC.
How can I work with these data sets in a loop such as
for a=1:N
B=A{N} *2
end
where N changes at each iteration and inserts the next data set saved with a different name.
Thanks, Cheers.
0 件のコメント
採用された回答
Ameer Hamza
2021 年 2 月 13 日
It is never a good idea to create a variable name like this: A1, A2, ..., AN. Read here: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. There is no good way to access values of these variables in a loop. It is better to create an array which is easier to loop over. For example, create a cell array
A = {A1, A2, A3, .., AN};
for i = 1:numel(A)
B = A{i}*2
end
2 件のコメント
VBBV
2023 年 3 月 19 日
A = {A1, A2, A3, .., AN};
for i = 1:numel(A)
B{i} = A{i}*2;
end
B
Use the for loop index i.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!