Sorting arrays of objects by dynamic property value

I have an number of face images that I have loaded into an object array for presentation using Matlab. Dynamic properties for the faces are added from a spreadsheet. All objects have the same properties, but there are a lot of them.
I have found that the normal manner of searching such an array (for instance by rated Anger) does not work with the dynamic properties.
[~,idx]=sort([faceArray.Anger]);
and
[~,idx]=sort([faceArray.('Anger')]);
both return the error:
No appropriate method, property, or field 'Anger' for class 'FaceStimulus'.
I can see the field in the variable viewer, and can sort by static properties using this method. Is there something that I'm missing?

4 Comments

obj.Property1
obj.('Property1')
Having investigated further, it seems that the more fundamental problem is that if obj is an array, that the above notation does not return all values of the dynamic property Property1. It behaves as expected when Property1 is static, or when obj is a singleton.
Is there some way around this?
According to the Method Invocation docs the dot notation should work: "For example, the following statements are equivalent:"
obj.Property1
obj.('Property1')
If this does not work you should contact to technical support.
Did you report this "issue" to technical support? I failed to find any hint on this limitation in the documentation.
Reproduction on R2016a:
sdp = SortingDynamicProperty();
sdp(end+1) = SortingDynamicProperty();
sdp(end+1) = SortingDynamicProperty();
%
sdp(1).addprop('dyna');
sdp(2).addprop('dyna');
sdp(3).addprop('dyna');
%
sdp(1).('dyna') = 'd1';
sdp(2).('dyna') = 'd2';
sdp(3).('dyna') = 'd3';
sdp(1).A = 'A1';
sdp(2).A = 'A2';
sdp(3).A = 'A3';
>> sdp(1).('dyna')
ans =
d1
>> { sdp.('dyna') }
No appropriate method, property, or field 'dyna' for class 'SortingDynamicProperty'.
>> { sdp.A }
ans =
'A1' 'A2' 'A3'
where
classdef SortingDynamicProperty < dynamicprops
properties
A
end
end
&nbsp
It seems that what you want (I also wanted to do it... ) is not possible diretly, see: http://de.mathworks.com/help/matlab/matlab_oop/object-arrays-with-dynamic-properties.html
where it says:
MATLAB® returns an error if you try to access the dynamic properties of all array elements using this syntax.
a.DynoProp
No appropriate method, property, or field 'DynoProp' for class 'ObjectArrayDynamic'.
Refer to each object individually to access dynamic property values:
a(1).DynoProp

Sign in to comment.

Answers (0)

Categories

Asked:

on 24 Jun 2016

Commented:

on 1 Aug 2016

Community Treasure Hunt

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

Start Hunting!