Modifying a structure array

87 views (last 30 days)
SS
SS on 11 Aug 2019
Edited: Bruno Luong on 26 Aug 2019
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.
  3 Comments
SS
SS on 12 Aug 2019
Hi Rik. The order doesn't matter, I just want them them to form a new column. It is from some biological process and I have to break the structure for a span of 50 units of a given field.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 12 Aug 2019
Edited: Bruno Luong on 12 Aug 2019
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.
  12 Comments
Bruno Luong
Bruno Luong on 26 Aug 2019
SS: "Hi again. I have tried to use the above code and unfortunately, it gives the follwoing error."
This error is expected to me because you wrote:
"For each S(i), the length of the field arrays is same throughout except 5 fields. The length of these 5 fields is 1 less than the other 25 field arrays. The last element in the these fields can be conisder as zero or empty, it doesn't matter."
Actually it does MATTER, contrary to what you wrote.
And I have warned you long ago.
"I let you deal with the exception of one less element on 5 fields (that's messy condition, and bad data structure design), you better fill them with trailing 0 or NaN, whatever suitable for you, to make all the fields have the same length."

Sign in to comment.

More Answers (1)

Rik
Rik on 11 Aug 2019
Edited: Rik on 11 Aug 2019
Since you don't provide any indication of how you want this to work for struct array input, you'll have to modify this code yourself.
S=struct;
S(1).f1=[2,3,4,5,6,7,8,10,14,16,18,20];
S(1).f2=[3,6,9,12,15,18,21,24,27,30,33,36];
L=S(1).f1<=10;
if sum(L)>=5
S(2).f1=S(1).f1(L);
S(2).f2=S(1).f2(L);
end
S(1).f1(L)=[];S(1).f2(L)=[];
Edit:
%create input struct
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;
%create the cell array that will hold the struct elements
c=cell(2,numel(S));
for n=1:numel(S)
c{1,n}=S(n);
L=S(n).f1<=50;
if sum(L)>=5
c{2,n}.f1=S(n).f1(L);
c{2,n}.f2=S(n).f2(L);
end
c{1,n}.f1(L)=[];c{1,n}.f2(L)=[];
end
%reshape back to a struct
S=[c{:}];
  4 Comments
Rik
Rik on 12 Aug 2019
The .() syntax works for char array inputs (and scalar strings).

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by