为什么“输出参数太多"?定义MATLAB容器时.输出0的Map子类方法? [英] Why "too many output arguments" when defining MATLAB containers.Map subclass method with 0 outputs?
问题描述
我试图通过对它进行子类化并添加具有0个输出的附加方法来扩展MATLAB container.Map类,但是在执行该方法时遇到输出参数过多"错误.这不是新方法的实现特有的-扩展容器的任何其他方法.输出为0的Map()都会产生此错误.
I am trying to extend the MATLAB containers.Map class by subclassing it and adding an additional method with 0 outputs, but I encounter the "too many output arguments" error when executing the method. This is not specific to the new method implementation--any additional method that extends containers.Map() with 0 outputs will generate this error.
具体来说,执行时会遇到错误
Specifically, the error is encountered when executing,
obj = Containers();
obj.testfun();
对于以下类定义,
classdef Containers < handle & containers.Map
methods
% Test function to display keys.
function testfun(obj)
obj.keys(); % dumby thing to execute with incoming object.
disp('it works!');
end
end
end
但是,如果我们稍加修改以输出至少一个参数,
However, if we slightly modify it to output at least one argument,
classdef Containers < handle & containers.Map
methods
% Test function to display keys.
function dumby = testfun(obj)
obj.keys();
disp('it works!')
dumby = 1;
end
end
end
它将正确执行.该错误似乎是在对container.Map进行子类化时专门发生的.如果有关系,我正在使用MATLAB R2014b.关于如何解决此问题有什么建议吗?
It will execute correctly. The error appears to happen specifically when subclassing containers.Map. If it matters, I am using MATLAB R2014b. Any suggestions as to how to resolve this issue?
推荐答案
不要尝试对containers.Map
进行子类化.这只是我的意见,但是MathWorks应该做到了,所以您就做不到了.
Don't try to subclass containers.Map
. This is just my opinion, but MathWorks should have made it Sealed
so that you just couldn't.
这里发生的是,当您键入a.b
或a(b)
或a{b}
时,这将转换为对方法subsref
的调用.所有对象都具有该方法.有关MATLAB如何准确调用subsref
的详细信息,请参见doc subsref
,但请注意-这非常复杂.
What's going on here is that when you type a.b
, or a(b)
or a{b}
, that is translated into a call to the method subsref
. All objects have that method. See doc subsref
for more detail on exactly how MATLAB calls subsref
but be warned - it's quite complicated.
现在,如果您想自定义类的行为,则可以通过提供自己的实现来重载subsref
. containers.Map
这样做是因为它需要支持通过键进行索引,这在其他MATLAB类中并不常见.
Now if you want to customize the behaviour of your class, you can overload subsref
by providing your own implementation. containers.Map
does this, as it needs to support indexing by keys, which is not typical for other MATLAB classes.
我无法确切地告诉您containers.Map
的subsref
是如何实现的,因为它是内置类,所以我看不到代码.但是我猜测,从您在此处看到的行为来看,它正在某个时刻试图确定.testfun()
是对方法testfun
的调用,还是对属性testfun
的调用,还是对字段
I can't tell you precisely how subsref
for containers.Map
is implemented, as it's a built-in class so I can't see the code. But I'm guessing, from the behaviour you're seeing here, that at some point it's trying to work out whether .testfun()
is a call to a method testfun
, or a property testfun
, or a field testfun
, or something else, and one of the things it's doing is to see whether it is being called with output arguments and changing its behaviour accordingly.
这是一个很好的示例,说明在不知道(可能无法修改)其内部的情况下,您实际上无法真正将其子类化的原因,这就是为什么我建议MathWorks最好将其做成Sealed
来防止您尝试.无论如何,如果没有一些解决方法,就无法使用所需的行为对它进行子类化(例如,为Containers
类实现自己的重载subsref
以便使其按您希望的方式工作)以及一些逆向工程(例如,对containers.Map
的subsref
方法的内部进行第二次猜测).
This is a great example of where you're not really able to subclass the object without knowing (and possibly modifying) its internals, which is why I suggest that MathWorks might better have made this Sealed
to prevent you from trying. In any case, you're not going to be able to subclass it with the behaviour you want, without a few workarounds (i.e. implementing your own overloaded subsref
for your Containers
class in order to make it work how you want) and a bit of reverse-engineering (i.e. second-guessing the internals of the subsref
method of containers.Map
).
相反,您可以尝试使用适配器模式,方法是使用containers.Map
作为私有(可能是隐藏的)属性创建自己的类,然后实现仅通过其参数传递并输出到基础
Instead, you might try using an Adapter pattern, by creating your own class with a containers.Map
as a private (maybe hidden) property, then implementing methods and properties that just pass through their arguments and outputs to the underlying containers.Map
. Finally, implement your own methods as well.
这篇关于为什么“输出参数太多"?定义MATLAB容器时.输出0的Map子类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!