oop - matlab subsref : {} with string argument fails, 为什么?

标签 oop matlab dictionary operator-overloading hashtable

Mathworks File Exchange 存储库中有一些哈希或字典类的实现。我看过的所有内容都使用括号重载来进行键引用,例如

d = Dict;
d('foo') = 'bar';
y = d('foo');

这似乎是一个合理的接口(interface)。但是,如果您想轻松拥有包含其他词典的词典,最好使用大括号 {} 而不是圆括号,因为这可以让您绕过 MATLAB 的(似乎是任意的)语法不允许使用多个括号但允许使用多个大括号的限制,即

t{1}{2}{3}  % is legal MATLAB
t(1)(2)(3)  % is not legal MATLAB

所以如果你想轻松地在字典中嵌套字典,

dict{'key1'}{'key2'}{'key3'}

as 是 Perl 中的一个常见习语,并且在包括 Python 在内的其他语言中可能并且经常有用,那么除非你想使用 n-1 中间变量来提取字典条目 n layers deep,这似乎是个不错的选择。重写该类的 subsrefsubsasgn 操作以对 {} 执行与之前对 相同的操作似乎很容易(),一切正常。

除了我尝试时没有。

这是我的代码。 (我已将其简化为最小情况。这里没有实现实际的字典,每个对象都有一个键和一个值,但这足以说明问题。)

classdef TestBraces < handle

    properties
        % not a full hash table implementation, obviously
        key
        value
    end

    methods(Access = public)


        function val = subsref(obj, ref)
            % Re-implement dot referencing for methods.
            if strcmp(ref(1).type, '.')
                % User trying to access a method             
                % Methods access
                if ismember(ref(1).subs, methods(obj))
                    if length(ref) > 1
                        % Call with args
                        val = obj.(ref(1).subs)(ref(2).subs{:});
                    else
                        % No args
                        val = obj.(ref.subs);
                    end
                    return;
                end                
                % User trying to access something else.
                error(['Reference to non-existant property or method ''' ref.subs '''']);
            end
            switch ref.type
                case '()'
                    error('() indexing not supported.');
                case '{}'
                    theKey = ref.subs{1};
                    if isequal(obj.key, theKey)
                        val = obj.value;
                    else
                        error('key %s not found', theKey);
                    end
                otherwise
                    error('Should never happen')
            end
        end    

        function obj = subsasgn(obj, ref, value)
            %Dict/SUBSASGN  Subscript assignment for Dict objects.
            %
            %  See also: Dict
            %

            if ~strcmp(ref.type,'{}')
                error('() and dot indexing for assignment not supported.');
            end

            % Vectorized calls not supported
            if length(ref.subs) > 1
                error('Dict only supports storing key/value pairs one at a time.');
            end
            theKey = ref.subs{1};
            obj.key = theKey;
            obj.value = value;
        end % subsasgn        
    end     
end

使用这段代码,我可以按预期分配:

t = TestBraces;
t{'foo'} = 'bar'

(很明显,t 的默认显示输出中的赋值工作。)所以 subsasgn 似乎可以正常工作。

但我无法检索值(subsref 不起作用):

t{'foo'}
??? Error using ==> subsref
Too many output arguments.

错误消息对我来说毫无意义,并且我的 subsref 处理程序的第一个可执行行处的断点从未命中,所以至少从表面上看这看起来像是 MATLAB 问题,而不是程序中的错误我的代码。

显然 () 括号下标的字符串参数 是允许的,因为如果您将代码更改为使用 () 就可以正常工作{}。 (除非你不能嵌套下标操作,这是练习的对象。)

无论是洞察我在我的代码中做错了什么,任何使我正在做的事情不可行的限制,或者嵌套字典的替代实现,我们都将不胜感激。

最佳答案

简答,将此方法添加到您的类中:

function n = numel(obj, varargin)
    n = 1;
end

编辑:长答案。

尽管 subsref 的函数签名出现在文档中的方式,它实际上是一个 varargout 函数 - 它可以产生可变数量的输出参数。大括号和点索引都可以产生多个输出,如下所示:

>> c = {1,2,3,4,5};
>> [a,b,c] = c{[1 3 5]}
a =
     1
b =
     3
c =
     5

subsref 预期的输出数量取决于索引数组的大小。在本例中,索引数组的大小为 3,因此有三个输出。

现在,再看看:

t{'foo'}

索引数组的大小是多少?另外 3. MATLAB 不关心您打算将其解释为字符串而不是数组。它只看到输入大小为 3,而您的 subsref 一次只能输出 1 个东西。因此,参数不匹配。幸运的是,我们可以通过重载 numel 改变 MATLAB 确定预期输出数量的方式来纠正问题。 .引用自文档链接:

It is important to note the significance of numel with regards to the overloaded subsref and subsasgn functions. In the case of the overloaded subsref function for brace and dot indexing (as described in the last paragraph), numel is used to compute the number of expected outputs (nargout) returned from subsref. For the overloaded subsasgn function, numel is used to compute the number of expected inputs (nargin) to be assigned using subsasgn. The nargin value for the overloaded subsasgn function is the value returned by numel plus 2 (one for the variable being assigned to, and one for the structure array of subscripts).

As a class designer, you must ensure that the value of n returned by the built-in numel function is consistent with the class design for that object. If n is different from either the nargout for the overloaded subsref function or the nargin for the overloaded subsasgn function, then you need to overload numel to return a value of n that is consistent with the class' subsref and subsasgn functions. Otherwise, MATLAB produces errors when calling these functions.

就是这样。

关于oop - matlab subsref : {} with string argument fails, 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8713730/

相关文章:

flash - 扩展 Class 类是否合法?

python - 在 Numpy 中设置 *默认* 数据顺序(C 与 Fortran)

perl - 如何从在 Ubuntu 上运行的 Simulink Standalone 可执行文件传递输入参数和记录输出

matlab - 设置 Matlab 相机 View (MoSTLy Zoom)

c# - 如何使用 Linq to XML 将 XML 转换为嵌套字典?

c# - 使用对象访问私有(private)方法

php - 使用自定义类名访问静态变量

Python:理解循环

python - 从列表列表创建字典

java - 在数组列表中的各种对象上调用子方法