Storing data from a for loop into a structure
3 次查看(过去 30 天)
显示 更早的评论
I am trying to store all data into a single structure. The data is currently generated in a for loop cycling through various data sets (with the name of each set stored in names1 as 19x1 cell of strings). The code then runs a large number of functions to load in data and produce the desired outputs PreDonn, PostDonn etc. which I want to store in this single structure, with the field of the structure defined based on the name of each set from names1. I am certain I have used this approach in different code before and it has worked, but this time when the code runs my Output file only contains the values for the last loop, with the rest of the data missing. I believe it is writing over the Output file during each loop, but I don't know why. I have also attempted preallocating all of the desired fields of the structure but they got written over as well. Any thoughts?
for L=1:size(names1)
...
...
Output.(names1{L}).PreDonn=PreDonn;
Output.(names1{L}).PostDonn=PostDonn;
Output.(names1{L}).PreDoff=PreDoff;
Output.(names1{L}).PostDoff=PostDoff;
end
5 个评论
回答(1 个)
ScottB
2024-12-12
size(names1) has 2 elements so I think that is the problem:
names = cell(1,3);
names(1,1) = 'Jim'
names(1,2) = 'Bill'
names(1,3) = 'Tyron'
PreDonn = eye(3)
sz_names = size(names)
for L = 1:sz_names(2)
Output.(names{L}).PreDonn=PreDonn;
end
whos
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!