applying isempty to property of object that reside in array- without loops

3 views (last 30 days)
I have a class with only one property (named 'value'). I can instantiate an array like this:
objarray(1,10)=myclass()
which will create 10 objects of myclass with emty 'value' property. Next I set the 'value' property of the first and last object to 99.
objarray(1).value=99; objarray(end).value=99;
My question is: How do I get a logical array denoting an empty 'value' property without using loops.
Something akin to: j= isempty(objarray.value)

Answers (1)

Walter Roberson
Walter Roberson on 24 Feb 2012
arrayfun( @(IDX) isempty(objarray(IDX).value), 1:length(objarray))
However, this does use a loop; it is just syntactically hidden. It will not be faster than using a loop.
Hmmm... Try this:
cellfun('isempty', {objarray.value})
This will use a loop internally; it is just syntactically hidden. It might be faster than using a loop yourself.
  1 Comment
Mario Chang
Mario Chang on 24 Feb 2012
Thanks Walter,
the cellfun option does give the results I'm looking for. The big problem with that approach is that the data will be duplicated (internally by Matlab, when it makes the cell array). The 'value' property may be hundreds of thousands in length, and the array may have thousands of objects, so this may be too expensive.
I will try the arrayfun option.
Thanks again!!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!