Looping to create nested structure?

5 vues (au cours des 30 derniers jours)
Emma Janke
Emma Janke le 8 Mar 2021
Commenté : Emma Janke le 8 Mar 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!

Réponse acceptée

Stephen23
Stephen23 le 8 Mar 2021
Modifié(e) : Stephen23 le 8 Mar 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 commentaires
Stephen23
Stephen23 le 8 Mar 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 le 8 Mar 2021
This is exactly what I needed - thanks for your expertise!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by