Delete columns in a structure array

조회 수: 5 (최근 30일)
SS
SS 2021년 6월 28일
편집: SS 2021년 6월 29일
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')

채택된 답변

Stephen23
Stephen23 2021년 6월 28일
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
Stephen23
Stephen23 2021년 6월 29일
"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 2021년 6월 29일
편집: SS 2021년 6월 29일
Thanks a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by