MATLAB Answers

problem explicitly calling subsasgn with buitlin method

4 views (last 30 days)
Andreas Klotzek
Andreas Klotzek on 5 May 2017
Edited: James Lebak on 26 May 2017
I have a problem when I try to overload the subsasgn method in a class. This is simple example class to show the problem:
classdef TestClass < handle
FigHandle = figure(1);
function obj = TestClass()
function val = getFigure( obj )
val = obj.FigHandle;
function obj = subsasgn( obj, s, b )
% redirect to builtin
obj = builtin( 'subsasgn', obj, s, b );
Now I am used to doing stuff like this:
t = TestClass()
t.getFigure().Units = 'normalized'
This will give the following error code:
Error using TestClass/getFigure
Too many input arguments.
Error in TestClass/subsasgn (line 21)
obj = builtin( 'subsasgn', obj, s, b );
Error in myTestScript (line 6)
t.getFigure().Units = 'normalized'
When I do not overload the subsasgn method, this call is going through.
What am I doing wrong?
Update: This is working properly: (without the empty braces on the method call)
t.getFigure.Units = 'normalized'


Sign in to comment.

Answers (2)

Guillaume on 5 May 2017
This is interesting. There's definitively something not working properly there. Experimenting with your test case, I changed the getFigure method to:
function val = getFigure( obj, varargin )
val = obj.FigHandle;
Since that's the function throwing the error Too many input arguments. With that simple change, running your test crashes matlab (R2017a and R2016b, WIN64). This is worthy of a bug report to mathworks.
Of course, that doesn't help with your situation, but it looks like you've found a bug in matlab.

  1 Comment

Andreas Klotzek
Andreas Klotzek on 8 May 2017
Thank you.
I also have a crash on R2015b with this change. I will report this directly to mathworks.

Sign in to comment.

Philip Borghesani
Philip Borghesani on 5 May 2017
You bumped into an area of loosely defined behavior.
In general you can't index into the results of a function call in MATLAB. There are a few places where it is allowed to work for compatibility with Java and Java algorithms and to avoid breaking too much old code.
The correct way to write this code in MATLAB is
fig=t.getFigure(); %call function getFigure;
Or to directly access the FigHandle property which is fine: t.FigHandle.Units = 'normalized'. You could even add a get access method for the property or create a transient property with a get method that returns FigHandle.
I cant promise that when the crash is fixed the code you originally wrote won't error in a reasonable way instead of crashing.


Andreas Klotzek
Andreas Klotzek on 8 May 2017
Thanks for your answer.
I already had the impression that I might be doing something that matlab does only support by chance. It could still be some sort of bug because it is working when I am not overloading the method. I will see what mathworks will tell me when I report it.
I guess I will have to adapt to the style you pointed out in the future to be save.
James Lebak
James Lebak on 8 May 2017
The crash is definitely a bug in MATLAB and we will fix it in a future release. I am sorry for the inconvenience.
As Phil said indexing into the results of a method is generally not supported. However, custom subsasgn can give you something like the behavior you want (and also work around the bug) by recognizing the call to 'getFigure' and forwarding the subsequent indexing to the figure. For example:
function obj = subsasgn( obj, s, b )
if (numel(s) == 1 && isCallToGetFigure(s(1))) || ...
(numel(s) == 2 && isCallToGetFigure(s(1)) && ...
% obj.getFigure = b or obj.getFigure() = b;
obj.FigHandle = b;
elseif numel(s) > 2 && isCallToGetFigure(s(1)) && ...
% obj.getFigure() with subsequent indexing
obj.FigHandle = builtin( 'subsasgn', obj.getFigure, ...
s(3:end), b );
elseif numel(s) > 1 && isCallToGetFigure(s(1))
% obj.getFigure with subsequent indexing
obj.FigHandle = builtin( 'subsasgn', obj.getFigure, ...
s(2:end), b );
error('invalid subsasgn syntax.');
The definitions of isCallToGetFigure and isEmptyParens would look like
function b = isCallToGetFigure(s)
b= strcmp(s.type,'.') && strncmp(s.subs,'getFigure',8);
function b = isEmptyParens(s)
b = strcmp(s.type,'()') && isempty(s.subs);

Sign in to comment.