Looping to create nested structure?
조회 수: 12 (최근 30일)
이전 댓글 표시
I am using code (provided to me) where data analysis consists of first converting data chunks to structures to run further functions.
I want to run multiple iterations (from different time points within the data) without manually creating a new structure for each time point.
I started with relevant data from time points into a cell array... this works
%%
nml = length(T1);
resp =cell(1,nml);
for i=1:nml;
resp{i} = BM.baselineCorrectedRespiration(1,T1(i):T2(i));
end
%%
Then I wanted to build a structure that contains all the structures corresponding to these time points for analysis
%%
Tot = struct;
for i=1:length(nml)
Tot.bm(i) = breathmetrics(resp{i},Fs,dataType);
end
%%
I tried this but it will only store one structure from the first pair of time points listed and will not continue to add my other datapoints. However, I can add manually to the structure, I'm just not sure how to automate.
Any thoughts?
Thank you!
댓글 수: 0
채택된 답변
Stephen23
2021년 3월 8일
편집: Stephen23
2021년 3월 8일
"...without manually creating a new structure for each time point."
Creating individual structures by hand should definitely be avoided!
Probably the best approach would be to use one single non-scalar structure:
Assuming that each of the scalar structures returned by that code have the same fields, then is is very easy to create the non-scalar structure after the loop (assuming that the scalar structures are stored in a cell array named C):
S = [C{:}]
After that you can use basic indexing to access each structure, e.g. the 2nd structure:
S(2)
댓글 수: 2
Stephen23
2021년 3월 8일
Something like this:
N = numel(T1);
C = cell(1,N);
for k = 1:N
tmp = BM.baselineCorrectedRespiration(1,T1(i):T2(i));
C{k} = breathmetrics(tmp,Fs,dataType);
end
S = [C{:}]
추가 답변 (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!