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. Related SolutionsMATLAB: Removing structure field if fn1 = fieldnames(Struct1);fn2 = fieldnames(Struct2);tf = ismember(fn2, fn1);NewStruct = struct();for K = 1 : length(tf) if tf(K); NewStruct.(fn2{K}) = Struct2.(fn2{K}); endendI expect that is also a vectorized formulation that uses struct2cell() MATLAB: How to delete n element from structure % Make a structure with one field% and an array in that field.s.field1 = [1,2,3,4,5];% Delete element 4 from the array in field1s.field1(4) = [] Related QuestionCreate struct with nx1 dimensionHow to set missing fields in a structure array to default values stored in another structure array (with same field names)How to make a structure to be input of a function and then its updated version to be output of the functionHow to add a new field in astructHow tho find the max value in a struct
Best Answer