How do I create a for loop with fields of structures?

9 ビュー (過去 30 日間)
MARGE
MARGE 2021 年 10 月 19 日
コメント済み: MARGE 2021 年 10 月 22 日
Hello. Im not an expert using matlab and this is my first time working with data in structures.
I have a structure that has another structure inside (see attached pictures) and I want to calculate the percentage of data that remain after filtering, my first approach is:
nt= length(Global_Data.FilteredData)
for i=1:nt
F= length(Global_Data.FilteredData(i).DTTM)
R= length(Global_Data.FreeAna_data(i).DTTM)
P= (F/R)*100
end
But with these I only get one value of P, and I need the percentage for each item (118)
Thanks in advance for your suggestions

採用された回答

Kelly Kearney
Kelly Kearney 2021 年 10 月 19 日
In your current code, you're saving over P on each iteration of the loop. Instead, save to an array:
nt= length(Global_Data.FilteredData);
P = zeros(nt,1);
for i=1:nt
F= length(Global_Data.FilteredData(i).DTTM)
R= length(Global_Data.FreeAna_data(i).DTTM)
P(i)= (F/R)*100
end

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 10 月 19 日
You can try to work with fieldnames() and getfield() names, e.g.:
Fnames = fieldnames(Global_Data);
for ii = 1:length(Fnames)
Fi = getfield(Global_Data,Fnames{ii});
...
P = ...;
end

カテゴリ

Help Center および 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