How to overload only "(" in subasgn?

5 ビュー (過去 30 日間)
Benedict
Benedict 2013 年 10 月 19 日
回答済み: Benedict 2013 年 10 月 19 日
I have implemented a class inheriting from handle and I would like to override only the parenthesis subref and sugasgn operations. I have overloaded the respective functions, but the problem is that now I cannot access the properties of the class:
M(j) = 5; % works
M.property1 = 2; % returns an error, since I did not treat the "." case in subasgn.m file
This class contains some complicated properties, and thus re-implementing manually every subreference (like . ( and { ) is simply infeasible. I tried to call subasgn@handle(), that is, literally passing the assignment to its parent, but this doesn't work (error 'subasgn' is not an accessible method of base class 'handle').
Any idea on how to deal with that?

回答 (2 件)

Daniel Shub
Daniel Shub 2013 年 10 月 19 日
I don't have access to MATLAB right now, so an untested solution should be to call the builtin method and not the handle method
function varargout = subsasgn(A, S, B)
switch S(1).type
case {'.', '{}'}
[varargout{1:nargout}] = builtin('subsasgn', A, S, B);
case '()'
disp('custom subsasgn')
end
end

Benedict
Benedict 2013 年 10 月 19 日
Thank you, works like a charm.

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by