Delete columns in a structure array

2 views (last 30 days)
SS
SS on 28 Jun 2021
Edited: SS on 29 Jun 2021
Hello.
I have a structure array Data (1 x 500,000) with 10 fields f1, f2,.......f10. I want to delete all the columns i.e., Data(i) whose f10 > 100.
For example, if the below is the input:
Data(1).f1=[10,70,30 40,50,60], Data(1).f2=[100,20,50,60,70,140],......Data(1).f10=[-10,20,-50,42,-70,140] ;
Data(2).f1=[16,98,74,47,99], Data(2).f2=[101,54,69,20,11],.......Data(2).f10=[17,-54,69,-20,37];
Data(3).f1=...... , Data(3).f2=.....,........ Data(3).f10=...........;
Data(4).f1=.... , Data(4).f2=....., ....... Data(4).f10=............;
.
.
Data(i).f1=...., Data(i).f2=.... and Data(i).f10=............;
In Data(1).f10 there is an entry 140 which is greater than 100 so, I want to delete the whole column Data(1).
I have tried below option but, it does not help.
for i=1:length(Data)
i
if (Data(i).f10 > 100)
Data(i)=[]
end
end
save('Data.mat')

Accepted Answer

Stephen23
Stephen23 on 28 Jun 2021
Assuming that the data in f10 is scalar numeric (you did not tell us this important information):
idx = [Data.f10]>10;
Data = Data(~idx)
save('Data.mat','Data')
  3 Comments
Stephen23
Stephen23 on 29 Jun 2021
"This is a bit confusing."
It is not very confusing: in your original question you omitted to give very important information, such as the sizes of the structure fields. Now that you have edited your question, we can make some progress:
fun = @(s) any(s.f10(:)>10);
idx = arrayfun(fun,Data);
Data = Data(~idx);
SS
SS on 29 Jun 2021
Edited: SS on 29 Jun 2021
Thanks a lot.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!