I have a struct with the following layout:
T X Y
[0,1] [0, 1] [0,5]
[0] [2] [2]
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
[0] [3] [3]
Is there a way to remove all NaN values such that I am left with only:
T X Y
[0,1] [0, 1] [0,5]
[0] [2] [2]
[0] [3] [3]
I am using isnan but keep getting an error "Undefined function 'isnan' for input arguments of type 'struct'".
Thanks

댓글 수: 4

Stephen23
Stephen23 2019년 5월 1일
@Manny Kins: please upload the structure in a .mat file by clicking the paperclip button.
Manny Kins
Manny Kins 2019년 5월 1일
Sure, the actual structure is quite large so I will remove some of the values then upload
Manny Kins
Manny Kins 2019년 5월 1일
Just added, thanks
Walter Roberson
Walter Roberson 2019년 5월 1일
Probably the easiest way is to use struct2cell and cellfun(@(C) any(isnan(C(:)), thecell), and then any() that across the proper dimension, to arrive at a logical vector of location to remove. Then delete those from the original struct.

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

 채택된 답변

Stephen23
Stephen23 2019년 5월 1일
편집: Stephen23 2019년 5월 1일

2 개 추천

This works for any sized fields (including empty), and gives you the choice of matching ALL or ANY of the fields containing NaN.
>> F = @(s)all(structfun(@(a)isscalar(a)&&isnan(a),s)); % or ANY
>> X = arrayfun(F,AllData.Passive);
>> AllData.Passive(X) = [];
checking:
>> AllData.Passive.T
ans =
0 1 2 3 4 6 7 8 9 10
ans =
0 3 4 7 9 10
ans =
0 2 6
ans =
0 1 6 7 8
ans =
10
ans =
10
ans =
10
ans =
10

댓글 수: 2

Manny Kins
Manny Kins 2019년 5월 2일
Thank you Stephen, this is really useful and flexible. I need to learn more about how and when to use the structfun and arrayfun functions as they seem to be very powerful
Yago Molano Gomez
Yago Molano Gomez 2020년 4월 7일
Hi! I've tried to copy this into my code but changing the empty brackets for a zero, and I get an error that says 'assignment between unlike types is not allowed'.
Also, when I use it with the empty brackets, it does not give me an error but does not remove the NaN's. How can I fix it? I'll attach the structures I'm referring to.

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

추가 답변 (2개)

Jos (10584)
Jos (10584) 2019년 5월 1일

1 개 추천

TF = arrayfun(@(k) isnan(AllData.Passive(k).T(1)), 1:numel(AllData.Passive))
AllData.Passive(TF) = []

댓글 수: 1

Manny Kins
Manny Kins 2019년 5월 2일
편집: Manny Kins 2019년 5월 2일
Thank you for your response Jos! Ahh Noddy (Noddy), the little man with the red and yellow car, good memories

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

Felipe Ademir aleman hernandez
Felipe Ademir aleman hernandez 2020년 4월 8일
편집: Walter Roberson 2020년 4월 8일

0 개 추천

Hey, this works for me:
MyNewDataStruct = structfun( @rmmissing , MyDataStruct , 'UniformOutput' , false)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2019년 5월 1일

편집:

2020년 4월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by