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 Center および File ExchangeStructures についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by