Looping to create nested structure?

5 Ansichten (letzte 30 Tage)
Emma Janke
Emma Janke am 8 Mär. 2021
Kommentiert: Emma Janke am 8 Mär. 2021
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!

Akzeptierte Antwort

Stephen23
Stephen23 am 8 Mär. 2021
Bearbeitet: Stephen23 am 8 Mär. 2021
"...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 Kommentare
Stephen23
Stephen23 am 8 Mär. 2021
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 am 8 Mär. 2021
This is exactly what I needed - thanks for your expertise!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by