How can I put a struct output into the i-th field of a struct on MATLAB?
1 回表示 (過去 30 日間)
古いコメントを表示
I have a for loop that runs a simulation 100 times. The results of that simulation go into a struct. For simulation run I have out.S, out.I, out.R and I want to then store the results of each simulation within another struct or array so I can then work out the average output across the 100 simulations.
Anyone any ideas of how to store the output from a for loop using structs? I usually use arrays but this one requires structs.
0 件のコメント
回答 (2 件)
madhan ravi
2018 年 11 月 23 日
編集済み: madhan ravi
2018 年 11 月 23 日
Use dot indexing to create a struct() , see https://www.mathworks.com/matlabcentral/answers/321489-how-can-i-create-a-structure-with-a-loop#answer_251562
for ...
S.result(i)... % S is your struct
end
0 件のコメント
Luna
2018 年 11 月 23 日
編集済み: Luna
2018 年 11 月 23 日
Hi,
You can do it as follows:
After that you have out.S as an array so you can assign it to any other struct.
for i =1:NumberOfSimulations
out.S(i) = simResult1;
out.I(i) = simResult2;
out.R(i) = simResult3;
end
or you can do below:
Your outputs will be different struct arrays.
for i = 1:NumberOfSimulations
S.result(i) = out.S;
I.result(i) = out.I;
R.result(i) = out.R;
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!