Info

この質問は閉じられています。 編集または回答するには再度開いてください。

assigning a number to a specific struct (1 to 9) using a for loop

1 回表示 (過去 30 日間)
JamJan
JamJan 2018 年 5 月 31 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
In my research I have several participants, named AT_110001 to AT_110009. I have made structures containing data, however I want to add a field to each one with a for loop. How I did this until now:
for i = 1:9
[AT_11000(i)(:).Sheet] = deal(['Sheet', num2str(i)]);
end
end
But this does not work, how can I make this work?
  1 件のコメント
Stephen23
Stephen23 2018 年 5 月 31 日
Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Red this to know why:
The simpler solution is to use a non-scalar structure, which makes this task trivially easy with indexing.

回答 (1 件)

Ameer Hamza
Ameer Hamza 2018 年 5 月 31 日
This type of AT_11000(i)(:) chained indexing is not supported in MATLAB. Also, you didn't mention the fields of your struct object. I assume you want to do something like this
[AT_11000.Sheet] = deal(['Sheet', num2str(i)]);
without the for loop. The syntax is correct, but I don't know it is doing what you want because you didn't share any sample dataset.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by