MATLAB: Simple operations with struct

structstructures

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?

Best Answer

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.