Ignoring empty arrays when using arrayfun on a structure?

조회 수: 5 (최근 30일)
bio lim
bio lim 2015년 8월 16일
댓글: bio lim 2015년 8월 16일
I have 1x2269 structure with 20 fields called data. I am trying to extract with a condition using arrayfun.
west_east = arrayfun(@(n) 110 >= data(n).TTA(1) & data(n).TTA(1) >= 70, 1 : length(data));
data = data(west_east);
The problem is that some of the arrays in my structure are empty. When I run the code, I am getting the error
Attempted to access data2.TTA(1); index out of bounds because
numel(data2.TTA)=0.
I understand that this is due to having empty cells in my structure such as
data2(9)
ans =
type: 'B738'
time: [0x1 double]
lat: [0x1 double]
lng: [0x1 double]
altitude: [0x1 double]
selected_altititude: [0x1 double]
BPS: [0x1 double]
RA: [0x1 double]
TTA: [0x1 double]
GS: [0x1 double]
TAR: [0x1 double]
TAS: [0x1 double]
heading: [0x1 double]
IAS: [0x1 double]
Mach: [0x1 double]
BAR: [0x1 double]
IVV: [0x1 double]
wind: [0x1 double]
real_time: []
instantaneous_GS: []
displacement: []
How can I adjust my code to get what I want? Thanks.

채택된 답변

David Young
David Young 2015년 8월 16일
Try this (not yet tested):
west_east = arrayfun(@(d) ~isempty(d.TTA) && 110 >= d.TTA(1) && d.TTA(1) >= 70, data);
data = data(west_east);
Notes:
  • I assume that the elements with empty arrays should be rejected like the ones that fail the test.
  • You don't need to apply arrayfun to an array of indices then index the array within the anonymous function. You just apply arrayfun to the array directly, and the anonymous function is applied to each element in turn.
  • When combining logical scalars (as here) it's more efficient to use && rather than &. In this case it's essential, in order to avoid accessing the TTA field if it's empty.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by