How can I change struct name in a loop

조회 수: 21 (최근 30일)
Ziv Kassner
Ziv Kassner 2018년 6월 18일
편집: Stephen23 2023년 9월 12일
Hi everybody,
I have a struct name: Individual1.length I want the user to enter a number and the script will change accordingly, for example:
interface: X = input('which individual? ');
user: X = 3;
interface: individual3.length
Hope it is clear enough,
Thanks,
Ziv

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 6월 18일
Although there is a MATLAB function to do this, but it is a very bad practice to name your variables like this. It will make your code error prone and hard to debug. Therefore I am going to suggest the correct way to solve the problem. You should create a struct array and store the input number as a field of the struct. For example
for ii=1:10
X = input('which individual? ');
s(i).individualNumber = X;
s(i).length = ...;
end
It will take 10 inputs from user and store them in struct array.
  댓글 수: 2
Stephen23
Stephen23 2018년 6월 18일
편집: Stephen23 2018년 6월 18일
Ziv Kassner
Ziv Kassner 2018년 6월 18일
Thank you very much.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by