MATLAB: Want to create new structs based off certain filter/conditions

copyingstructs

So I am writing a function that takes in input two structures, each are 1×1, with many fields inside of each. I want to output two new resulting structs which only contain the beams where the following conditions are true… El_Angle = 0, Taper = 4, Temperature = 20 and some more conditions as such. The new resulting structures will have the same fields as the input structures, but only contain beams where the conditions are met.
function [BeamlistNew,PattStatsNew] = BeamsToTest(Beamlist,PattStats)
I have been able to obtain location indices (using find) and then attempted with for loops to traverse through the structure and fields and then did something like :
a=1:numel(Beamlist.El_Angle);
b = find(Beamlist.Temperature(a) = 15);
BeamlistNew.Temperature = Beamlist.Temperature(b).
This works but is innieficent as I would have to do that for every single field. Also, I don't know how to use these locations for fields that don't require a condition (not every single field needs a condition met). Also, these conditions are only for the first struct and the second new struct should keep same format as input struct, but only with beams as aforementioned. Any other pointers or advice would be greatly appreciated.

Best Answer

N = length(Beamlist.El_Angle);
mask = true(1, N);
mask = mask & Beamlist.El_angle == 0;
mask = mask & Beamlist.Taper == 4;
mask = mask & Beamlist.Temperature >= 18 & Beamlist.Temperature <= 22;
etc
BeamlistNew = structfun(@(M) M(mask), Beamlist, 'uniform', 0);
No need to process the fields individually.