problem explicitly calling subsasgn with buitlin method

3 visualizaciones (últimos 30 días)
Andreas Klotzek
Andreas Klotzek el 5 de Mayo de 2017
Editada: James Lebak el 26 de Mayo de 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'

Respuestas (2)

Guillaume
Guillaume el 5 de Mayo de 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 comentario
Andreas Klotzek
Andreas Klotzek el 8 de Mayo de 2017
Thank you.
I also have a crash on R2015b with this change. I will report this directly to mathworks.

Iniciar sesión para comentar.


Philip Borghesani
Philip Borghesani el 5 de Mayo de 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 comentarios
James Lebak
James Lebak el 8 de Mayo de 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)) && ...
isEmptyParens(s(2)))
% obj.getFigure = b or obj.getFigure() = b;
obj.FigHandle = b;
elseif numel(s) > 2 && isCallToGetFigure(s(1)) && ...
isEmptyParens(s(2))
% 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 );
else
error('invalid subsasgn syntax.');
end
The definitions of isCallToGetFigure and isEmptyParens would look like
function b = isCallToGetFigure(s)
b= strcmp(s.type,'.') && strncmp(s.subs,'getFigure',8);
end
function b = isEmptyParens(s)
b = strcmp(s.type,'()') && isempty(s.subs);
end
James Lebak
James Lebak el 26 de Mayo de 2017
Editada: James Lebak el 26 de Mayo de 2017
I wanted to include a link to the bug report for completeness.

Iniciar sesión para comentar.

Categorías

Más información sobre Desktop en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by