Dear all,
I have a part of my code that I would like to clean a bit and make it more user friendly:
A = initvals(1);
B = initvals(2);
C = initvals(3);
D = initvals(4);
.
.
.
The variables A, B, C, D etc (I have 200 variables) are calling the values stocked into "initvals". How can I automise this by avoiding having 200 lines for all variables and having an automatic numbering inside initvals (not having to do it by hand for each variables)?
Thank you!

댓글 수: 2

John D'Errico
John D'Errico 2023년 2월 21일
This sort of thing always leads to problems. Yes, you want to have all 200 variables created for you on the fly. It is always a bad idea.
Stephen23
Stephen23 2023년 2월 21일
"How can I automise this by avoiding having 200 lines for all variables..."
Use a loop and one container array (e.g. a cell array).
"... and having an automatic numbering inside initvals"
Use a loop. Loops are a very basic feature of MATLAB (and every other language), and are introduced here:
"I have a part of my code that I would like to clean a bit and make it more user friendly... The variables A, B, C, D etc (I have 200 variables).."
And therein lies the unavoidable contradiction:

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2023년 2월 21일

0 개 추천

ic = num2cell(initvals);
[A, B, C, D etc] = deal(ic{:}) ;

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2023년 2월 21일

답변:

2023년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by