looping though data structures of different sizes

Hi Folks, please advise if you can....
Im extracting data from a structure in a for loop as follows:
for i = 1:1600
Struct = xml2struct(filename);
time(i) = Struct.time.v1);
data(i) = Struct.data.v2);
end
The problem is most of the Structures have 1 value for v1 and v2 but around 100 of the structures have multiple values. Hope can I deal with this in a loop and extract all the v1 and v2 values into one vector?
Thanks in advance for any advice!

2 件のコメント

Guillaume
Guillaume 2018 年 1 月 31 日
Well, what do you want to do when there's multiple values. Just store the first one? an average or other statistics? store all of them?
Note that calling a variable Struct is a bad idea. Personally, I'd call it filexml.
Chris O'Donnell
Chris O'Donnell 2018 年 1 月 31 日
store all of them! Thanks!

サインインしてコメントする。

 採用された回答

Jan
Jan 2018 年 1 月 31 日
編集済み: Jan 2018 年 1 月 31 日

0 投票

dataC = cell(1, 1600);
timeC = cell(1, 1600);
for i = 1:1600
S = xml2struct(filename);
timeC{i} = S.time.v1;
dataC{i} = S.data.v2;
end
timeV = cat(1, timeC{:}); % Or cat(2, ...
dataV = cat(1, dataC{:}); % Or cat(2, ...

4 件のコメント

Chris O'Donnell
Chris O'Donnell 2018 年 1 月 31 日
編集済み: Stephen23 2018 年 1 月 31 日
Works perfectly! Thanks very much for your help, really appreciate it!
Stephen23
Stephen23 2018 年 1 月 31 日
@Chris O'Donnell: you can thank Jan Simon by accepting their answer.
Jan
Jan 2018 年 1 月 31 日
You are welcome, Chris. Personally I like a "Thanks very much" more than pressing the "Accept" button. I have more points than I can ever exchange to coffee in my whole life. But accepting an answer tells other readers, that the problem is solved already. An this is very useful to let them focus on open problems.
Chris O'Donnell
Chris O'Donnell 2018 年 2 月 1 日
OK Jan, cheers

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by