Matlab subsref和end

时间:2017-02-06 18:13:25

标签: matlab operator-overloading

我有一个自定义矩阵类,我希望能够索引为:

x = myobj(1,2).d(3,4)     % myobj(1,2,3,4)
x = myobj(2, 3).d(3, end) % myobj(1,3,1,end)

我希望这些也能用于作业。

我开始时:

class MyClass < double
  methods
    function obj = MyClass(x)
      obj = obj@double(x);
    end
    function obj = subsref(obj, s)
      varargout{:} = subsref@double(obj, subsintercept(obj, s));
    end
    function obj = subsasgn(obj, s, b)
      obj = subsasgn@double(obj, subsintercept(obj, s), b);
    end
  end
end

然后我可以搞砸subsintercept中的索引。但是,我遇到了一个问题。实现最小化:

function s = subsintercept(obj, s)
    disp('subsintercept');
    for i = 1:length(s)
        disp(s(i));
    end
end

我得到了预期的行为

>> myobj = MyClass(zeros(1,2,3,4))
>> myobj(1,2).d(3,4)
subsintercept
type: '()'
subs: {[1]  [2]}
type: '.'
subs: 'd'
type: '()'
subs: {[3]  [4]}
<error due to not having finished subsintercept yet>

但这出乎意料的

>> myobj(1,2).d(3,end)
subsintercept
type: '()'
subs: {[1]  [2]}
type: '.'
subs: 'd'
<error due to not having finished subsintercept yet>
  1. 为什么添加end会导致我无法收到3
  2. 是否记录了此行为?

1 个答案:

答案 0 :(得分:0)

一些测试显示:

end_ = str2func('end');
d_temp = subsref(myobj, substruct('()', {1 2}, '.', 'd'));
ei = end_(d_temp, 2, 2);
result = subsref(myobj, substruct('()', {1 2}, '.', 'd', '()', {3, ei}));

翻译为:

subsref

即,subsref(d_temp, substruct('()', {3, ei}))被调用两次! *

*令人惊讶的是,第二次不是require 'nokogiri' doc = Nokogiri::HTML(<<EOT) <div class="comment"> <span class="c00"> TEXT <p>text</p> <p>text</p> <div class="reply">reply</div> </span> </div> EOT span = doc.at('span.c00') text_nodes = [ span.children.first.text.strip ] text_nodes += span.search('p').map(&:text) text_nodes # => ["TEXT", "text", "text"]