I have structure with 10 fields of the same size. The structure was read from a data file.
dataFile = ezread('myFile','\t');
I get a specific field (double) and produce an index array for all rows which are bigger than 2.
a_field = dataFile.a_field;
a = ismember(a_field,2:1000)
I could use ‘a’ to get all rows from another field where ‘a_field’ is bigger than 2. But how can I filter the complete structure? I would like to have a new structure which holds all rows from all fields where ‘a_field’ is bigger than 2.
It’s a basic question, but I can’t find an easy way around.
Structures store their fields separately, so you have to go and apply your filter to each.
For example, consider the following structure
Now you have two options to filter the other fields:
1) structfun
use STRUCTFUN to apply your filtering function on each field:
2) FOR-loop + dynamic field names
write an explicit for-loop, and use dynamic field names to access each:
3) cellarrays
Alternatively, we could convert the stucture to a cellarray, then apply the slicing to the entire thing:
The result in this case: