Ignoring empty arrays when using arrayfun on a structure?

5 views (last 30 days)
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.

Accepted Answer

David Young
David Young on 16 Aug 2015
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.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!