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

조회 수: 3 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by