problem explicitly calling subsasgn with buitlin method

3 次查看(过去 30 天)
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'

回答(2 个)

Guillaume
Guillaume 2017-5-5
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.

Philip Borghesani
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 个评论
James Lebak
James Lebak 2017-5-8
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by