Looping to create nested structure?

5 次查看(过去 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!

采纳的回答

Stephen23
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
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{:}]
Emma Janke
Emma Janke 2021-3-8
This is exactly what I needed - thanks for your expertise!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by