在MATLAB中为自定义类重载索引操作符`()`和`{}`

use*_*886 1 matlab operator-overloading operator-keyword

我希望能够在MATLAB中重载索引操作符 (){}我的类.特别是,我希望能够定义类方法,如...

% ...
classdef MyClass < handle

    % ... other class definition stuff...

    function returnVal = operator{}(indexRange)
        %....implementation here....
    end
    function returnVal = operator()(indexRange)
        %....implementation here....
    end
end
Run Code Online (Sandbox Code Playgroud)

这样我就可以创建对象并做...

x = MyClass();
returnedVals = x(:);
returnedCells = [x{:}];

% etc.
Run Code Online (Sandbox Code Playgroud)

这在MATLAB中是否可行?我知道这在C++和python中很容易(分别通过重载operator []__get__运算符).Mathworks网站本身并不太清楚如何做到这一点.

Tro*_*kin 5

你需要重载subsrefsubsasgn的内容功能classdef.Mathworks提供了它的工作原理的完整示例.请注意,如果您希望类中使用重载方法,则需要显式调用它.