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!
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!