Modifying a structure array
26 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Bruno Luong
2019-8-12
编辑:Bruno Luong
2019-8-12
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 个评论
Rik
2019-8-26
Neither code section produces the error you indicate. You should try to find a data line that causes this error. That is one of the hallmarks of an MWE: the smallest section of data and code that produces the error.
Often when trying to create an MWE you already find the mistake on your own. I have sometimes started writing my own question, when I found the mistake when creating an MWE for my post.
Bruno Luong
2019-8-26
编辑:Bruno Luong
2019-8-26
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."
更多回答(1 个)
Rik
2019-8-11
编辑:Rik
2019-8-11
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!