フィルターのクリア

problem explicitly calling subsasgn with buitlin method

1 回表示 (過去 30 日間)
Andreas Klotzek
Andreas Klotzek 2017 年 5 月 5 日
編集済み: James Lebak 2017 年 5 月 26 日
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.
  1 件のコメント
Andreas Klotzek
Andreas Klotzek 2017 年 5 月 8 日
Thank you.
I also have a crash on R2015b with this change. I will report this directly to mathworks.

サインインしてコメントする。


Philip Borghesani
Philip Borghesani 2017 年 5 月 5 日
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
James Lebak
James Lebak 2017 年 5 月 26 日
編集済み: James Lebak 2017 年 5 月 26 日
I wanted to include a link to the bug report for completeness.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by