フィルターのクリア

checking if field values in a single struct are identical

19 ビュー (過去 30 日間)
Davindra Usov
Davindra Usov 2023 年 1 月 19 日
コメント済み: Davindra Usov 2023 年 2 月 1 日
Hello,
I have a struct (S) with 13 fields. Each field has a 'density' column. I want to do an if statement which checks if all the values in the density columns are the same for each field (all 13 of them) i.e. density vals in field 1 = density vals in field 2 etc
if...(need help with this line)
disp('same altitude')
else
disp('different heights')
end
thank you
  1 件のコメント
Jan
Jan 2023 年 1 月 19 日
編集済み: Jan 2023 年 1 月 19 日
What does this mean: "Each field has a 'density' column." Do the fields contain tables? Or is "density" the name of a subfield containing a clumn vector?
Prefer to post some Matlab code, which creates some small example data.

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

採用された回答

Jan
Jan 2023 年 1 月 19 日
Depending on how the inputs look exactly, maybe:
FieldC = struct2cell(S);
Eq = true;
for k = 2:numel(FieldC)
if ~isequal(FieldC{k}.density, FieldC{1}.density)
Eq = false;
break;
end
end
There are shorter methods, but please explain at first exactly, what the inputs are.
  3 件のコメント
Jan
Jan 2023 年 1 月 19 日
I assume, the shown code works for table objects also.
Davindra Usov
Davindra Usov 2023 年 2 月 1 日
yes, it does! many thanks

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 1 月 19 日
density_cell = structfun(@(C) {C.density}, YourStruct);
EQ = isequal(density_cell{:});
No explicit loop needed (but structfun is arguably a hidden loop.)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by