Reference changing field names in loop

18 ビュー (過去 30 日間)
Jack Foley
Jack Foley 2020 年 7 月 24 日
コメント済み: Bruno Silva 2022 年 1 月 6 日
Hello all,
I'm working with data coming from 32 probes of the same build, which comes in the form of *.tdms files, one file for every probe. I'm using a Script from this forum to extract the information into a structure:
for i=1:32
probe(i) = TDMS_getStruct(filename(i));
end
This returns a structure called "probe" with the fields "Property" and "chanel_x", where x is the number of the corresponding probe, and each of these fields has a few subfields. As an example, the data I'm looking for, in the case of the first probe and second probe respectivly, is found in:
probe(1).channel_1.data % data from probe 1
probe(2).channel_2.data % data from probe 2
My problem is, that I haven't found a way to access that data in a loop, because the field name "channel_x" is different for every probe and I havn't found a solution yet to call this field for every probe. I'm looking for something like:
for i=1:32
data(i) = probe(i).channel_(i).data; %this obviously doesn't work, this is just to show that I need the field name to change in every loop too
end
I know this is similar to changing a variable in every loop, which isn't good programming practice, but here I'm in the situation that I can't do anything about the change in field name for every probe (that's just the way I get the data), so I'm hoping someone here can help me with this issue.

採用された回答

Arthur Roué
Arthur Roué 2020 年 7 月 24 日
編集済み: Arthur Roué 2020 年 7 月 24 日
You can access a field dynamically by putting the name of the field in a var in brackets.
for i=1:32
field = sprintf('channel_%d', i)
data(i) = probe(i).(field).data;
end
  2 件のコメント
Jack Foley
Jack Foley 2020 年 7 月 24 日
Thank you so much, that worked perfectly!
Bruno Silva
Bruno Silva 2022 年 1 月 6 日
Thankk you! It helped me also :D

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by