Beginner: How do I loop through strings ?

I have the following code:
br1=cell2mat(br1);
br2=cell2mat(br2);
...
br49=cell2mat(br49);
br50=cell2mat(br50);
How can I loop from br1 to br50? I tried using for n=1:50 eval('br' + n '=cell2mat(br' + n + ');'); but it doesn't work ("error using "+", incompatible array size").

 採用された回答

Jan
Jan 2022 年 10 月 17 日

0 投票

This is the question asked most frequently by beginners. The answer of the experts is always the same: Don't do this.
This is exhaustively explained here: TUTORIAL: Why and how to avoid Eval
Do not hide an index in the name of a variable, but use an array and an index:
brC = cell(1, 50);
for k = 1:50
brC{k} = cell2mat(br(k));
end
This is some pseudo-code only, because it is not clear, what the contents of your variables is.

1 件のコメント

Giulio Gottardo
Giulio Gottardo 2022 年 10 月 20 日
Thank you! This is most helpful

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

質問済み:

2022 年 10 月 17 日

コメント済み:

2022 年 10 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by