Overloading subsref and multiple outputs

3 次查看(过去 30 天)
I am able to overload subsref in my classes, but I have noticed some rather odd behavior. I have implemented a very simple class, which you can see below. It has a single property.
If I have an array of these objects, and I access that property, I get multiple outputs. In fact, I get one output for each element in my array, as expected.
When I place a break point in the subsref function to observer what is going on, I notice that nargout evaluates to the number of elements in my object array, again, as expected.
The first unexpected thing is that if I index into to object array, I no longer get multiple outputs, that is, unless I specifically ask for multiple outputs.
The second unexpected thing I noticed, is that when I place a break point in the subsref function, nargout evaluates to zero, unless I specify before hand how many outputs I want.
This behavior has several less than ideal implications for me, which are not really related to the this question.
Is there any way to get the same behavior for the indexed object array as for the un-indexed array?
Any ideas are more than welcome.
Thanks.
>> obj = [MyClass MyClass MyClass]; % Now I have a 1x3 object array
>> obj.prop
ans =
0
ans =
0
ans =
0
>> obj(1:3).prop
ans =
0
>> [a,b,c] = obj(1:3).prop
a =
0
b =
0
c =
0
>> x = [obj.prop]
x =
0 0 0
>> y = [obj(1:3).prop]
y =
0
>>
-------------------------------------------------------------------------------------
classdef MyClass < handle
properties
prop = 0;
end
methods
function varargout = subsref(this,S)
[varargout{1:nargout}] = builtin('subsref',this,S);
end
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by