problem explicitly calling subsasgn with buitlin method

5 views (last 30 days)
Andreas Klotzek
Andreas Klotzek on 5 May 2017
Edited: James Lebak on 26 May 2017
Hi
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
properties
FigHandle = figure(1);
end
methods
function obj = TestClass()
end
function val = getFigure( obj )
val = obj.FigHandle;
end
function obj = subsasgn( obj, s, b )
% redirect to builtin
obj = builtin( 'subsasgn', obj, s, b );
end
end%methods
end%classdef
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'

Answers (2)

Guillaume
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;
end
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;
fig.Units='normalized';
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.
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!