How can I change an array name within a for loop using the value of the index?

17 ビュー (過去 30 日間)
Derek Keglovits
Derek Keglovits 2022 年 3 月 29 日
回答済み: Tala 2022 年 3 月 29 日
for i=1988:2020;
months_i=dates_i.Month;
end
I want the months_i to be read as months_1988, then months_1989, etc because I'm looking at arrays from those ~20 years and am digging into each array with multiple loops underneath. Thank you!
  1 件のコメント
AndresVar
AndresVar 2022 年 3 月 29 日
編集済み: AndresVar 2022 年 3 月 29 日
You can do it with eval but it’s not recommended. Use a cell array instead or another struct
See below
https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval?s_tid=srchtitle

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

回答 (1 件)

Tala
Tala 2022 年 3 月 29 日
for i= 1988:2020
month{i} =['month_',num2str(i)];
end

カテゴリ

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