Simple operations with struct

11 ビュー (過去 30 日間)
Jórdan Venâncio Leite
Jórdan Venâncio Leite 2020 年 4 月 5 日
コメント済み: Image Analyst 2020 年 6 月 11 日
How to exclude elements of a struct with a value less than a limit?
For example, if a struct has only one field and this field has the values 220000 and 40000. How to exclude (make 0) only the values less than 50000?
  1 件のコメント
Image Analyst
Image Analyst 2020 年 6 月 11 日
Original question before it gets edited away:
How to exclude elements of a struct with a value less than a limit?
For example, if a struct has only one field and this field has the values 220000 and 40000. How to exclude (make 0) only the values less than 50000?

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 5 日
編集済み: Ameer Hamza 2020 年 4 月 5 日
idx = [struct_name.fieldname] > 50000;
new_struct = struct_name(idx);
new_struct will only contain elements where the field name is greater than 50000.
If you don't want to delete those elements, and just want to set them to 0,
new_struct = struct_name;
idx = [new_struct.fieldname] > 50000;
[new_struct(idx).fieldname] = deal(0);
In this case, new_struct will have same elements as original struct_name but the elements set to 0.

その他の回答 (1 件)

David Hill
David Hill 2020 年 4 月 5 日
yourStuck.field(yourStuck.field<50000)=0;
  1 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 5 日
David, this syntax is not supported in MATLAB
s(1).a = 1;
s(2).a = 2;
s(3).a = 3;
s(4).a = 4;
s.a(s.a < 3) = 0;
Error using <
Too many input arguments.
Error in Test (line 6)
s.a(s.a < 3) = 0;

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by