Storing data from a for loop into a structure

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

hello
we don't know if the rest of your code is faulty for that , but the basic operations work as you stated.
Now I would replace size with numel to avoid issues when names1 cell array has dimensions 1 x N instead of N x 1 .
names1 = {'aa','bb','cc'};
for L=1:numel(names1)
Output.(names1{L}).PreDonn=rand(1,1);
Output.(names1{L}).PostDonn=rand(1,1);
Output.(names1{L}).PreDoff=rand(1,1);
Output.(names1{L}).PostDoff=rand(1,1);
end
Output
Output = struct with fields:
aa: [1x1 struct] bb: [1x1 struct] cc: [1x1 struct]
I have found my error. It was really stupid I had a "clearvars -except ..." somewhere and had just defined a new variable and forgotten to add it to the exception list.
Note that rather than creating lots of nested structures inside one scalar structure and using dynamic fieldnames, you could easily flatten and simplify your data by using just one non-scalar structure with basic indexing:
for L = 1:numel(names1)
...
Output(L).PreDonn=PreDonn;
Output(L).PostDonn=PostDonn;
Output(L).PreDoff=PreDoff;
Output(L).PostDoff=PostDoff;
Output(L).Name = names1{L}; % much more robust!
end
Simpler, more robust, and generally more efficent this also has benefits when accessing your data, e.g.:
excellent suggestion !
Thankyou :)

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

답변 (1개)

ScottB
ScottB 2024년 12월 12일

0 개 추천

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

댓글 수: 1

I don't think this is the issue. I've changed the code and it still has the same issue.
for L=1:size(names1,1)
...
Output.(names1{L,1}).PreDonn=PreDonn;
Output.(names1{L,1}).PostDonn=PostDonn;
Output.(names1{L,1}).PreDoff=PreDoff;
Output.(names1{L,1}).PostDoff=PostDoff;
end

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2024a

질문:

2024년 12월 12일

댓글:

2024년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by