MATLAB: Modifying a structure array

modifying a structure

Hi. I am working with a structure array, and I want to modify the structure based on a condition on the field entries.
For example,
Here is the input,
S(1).f1=[1:100] and S(2).f2=[2:101];
S(2).f1=[40:120] and S(2).f2=[60:140]
S(3).f1=...... and S(3).f2=.....
S(4).f1=.... and S(4).f2=.....
.
.
S(i).f1=.... and S(i).f2=....
Can anyone suggest me some smart way to do this.
Conditions on input Output
S(1).f1>=0 and S(1).f1<=50 then ——> S(1).f1=[1:50] and S(1).f2=[2:51]
S(1).f1>50 and S(1).f1<=100 then ——>S(2).f1=[51:100] and S(2).f2=[52:101]
…. so on
Now, the same process for original/input S(2).f1, S(3).f1 …. S(i).f1 to re-form the structure.
After, modification the final step is to delete S(i) if the size of S(i).f1 is < 5.

Best Answer

S=struct;
S(1).f1=5*[2,3,4,5,6,7,8,10,14,16,18,20];
S(1).f2=5*[3,6,9,12,15,18,21,24,27,30,33,36];
S(2).f1=40:120;S(2).f2=60:140;
S(3).f1=20:100;S(3).f2=40:120;
filtfun = @(s) s.f1>=0 & s.f1<=50; % adapt to your need
b = arrayfun(filtfun, S, 'unif', 0);
Sfilter = arrayfun(@(s,b) structfun(@(a) a(b{1}), s, 'unif', 0), S, b); % your reasult
I let you do the deletion step.