Bug in subsref overloading
Show older comments
Let us first show expected behaviour:
classdef GoodDemo
properties
st;
end
methods
function obj = GoodDemo()
obj.st = struct('x', {1,2,3});
end
end
end
good = GoodDemo;
disp(good.st(2).x); % --> 2
disp({good.st.x}); % --> [1] [2] [3]
However, when we overload subsref (just let it return the built-in output, nothing fancy) things break:
classdef BugDemo
properties
st;
end
methods
function obj = BugDemo()
obj.st = struct('x', {1,2,3});
end
function varargout = subsref(obj, s)
[varargout{1:nargout}] = builtin('subsref', obj, s);
end
end
end
bug = BugDemo;
disp(bug.st(2).x); % --> 2
disp({bug.st.x}); % --> [1]
The problem seems to be that in the overloaded subsref somehow `nargout == 1`. Anything I am doing wrong here?
I found a bit of a crazy workaround, and the most problematic thing about it is it is only supported from 2015b onwards. The workaroud is to also overload the `numArgumentsFromSubscript` with -- big surprise -- its built-in value:
classdef WeirdFixDemo
properties
st;
end
methods
function obj = WeirdFixDemo()
obj.st = struct('x', {1,2,3});
end
function varargout = subsref(obj, s)
[varargout{1:nargout}] = builtin('subsref', obj, s);
end
function n = numArgumentsFromSubscript(obj, s, ic)
n = builtin('numArgumentsFromSubscript', obj, s, ic);
end
end
end
weird = WeirdFixDemo;
disp(weird.st(2).x); % --> 2
disp({weird.st.x}); % --> [1] [2] [3]
This works for Matlab 2015b+. Is there a way to get this working for earlier versions of Matlab? I tried setting `nargout` explicitly, but that has no effect at all.
9 Comments
per isakson
on 8 Dec 2016
Edited: per isakson
on 8 Dec 2016
- I think this should be reported to the Tech Support, if only to increment a counter in their database.
- numArgumentsFromSubscript was introduced in R2015b. I vaguely recall a discussion on something similar at Answer or the newsgroup, but with numel
per isakson
on 8 Dec 2016
Edited: per isakson
on 12 Dec 2016
MATLAB calls overloaded subsref methods with
incorrect nargout when evaluating indexing
expressions that are not assigned to a variable
However, I wouldn't call your "big surprise" fixed.
per isakson
on 8 Dec 2016
Edited: per isakson
on 8 Dec 2016
- Matlab is a high profile product and by filing issues we show our high expectations.
- "be publicly accessible"   I'm on a university site license and logged in on "My Account"; my name is on display above the search field.
- "Any thoughts?"  Unfortunately, no.
Paul
on 8 Dec 2016
per isakson
on 8 Dec 2016
Edited: per isakson
on 8 Dec 2016
A Guide to MATLAB Object-Oriented Programming by Andy H. Register includes extensive discussions on subsasgn and subsref. The book was published the year before Matlab introduced the new Class Object System. That's really unfortunate timing.
per isakson
on 8 Dec 2016
"So I assume my bug report is "not publicly accessible" to you."   Your request will be processed. It will not be automatically included in the "bug-database". I believe that most issues are included, but that only an appropriate selection are on public display.
Walter Roberson
on 8 Dec 2016
Accepted Answer
More Answers (1)
Philip Borghesani
on 8 Dec 2016
3 votes
I don't think there is anything new here or any new bugs here. There was a design flaw with how numel was used to implement the number of outputs from subsref in a comma separated list context, that was fixed by adding the function numArgumentsFromSubscript in R2015b. There is no way to get your desired output in previous versions of MATLAB.
To maintain compatibility with existing classes that implemented subsref, numArgumentsFromSubscript is only used when it is implemented by a class if the class implements subsref. The builtin returns the number of outputs that would be expected from the indexing operation passed as inputs provided there was no subsref function.
1 Comment
Paul
on 9 Dec 2016
Edited: per isakson
on 9 Dec 2016
Categories
Find more on Customize Object Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!