向Matlab表类添加方法 [英] Add a method to Matlab table class
问题描述
我希望在table
类中添加一个名为nansubset
的方法.本质上,它允许您调用T(r,c)
,其中r
和c
是可能包含NaN
的实数正整数向量.
I wish to add a method called nansubset
to the table
class. Essentially it allows you to call T(r,c)
where r
and c
are real positive integer vectors possibly containing NaN
's.
nansubset.m
的残存代码可能是:
function T = nansubset(T, r, c)
T = T(r,c);
end
I am following the instructions here, which detail how to add a new method to the cell
class. Basically, in a folder on my Matlab path, I create a folder called @table
, and within this folder, create a file called nansubset.m
.
我遇到以下问题:
>> tmpT = table(); nansubset(tmpT, 1, 1)
Undefined function 'nansubset' for input arguments of type 'table'.
和
>> doc @table/nansubset
Your search - @table/nansubset - did not match any documents.
但是:
edit nansubset
和
edit @table/nansubset
在我的编辑器中都打开方法文件.
both open the method file in my editor.
此外,我按照上面的链接中的说明将plus
方法添加到cell
类中,并发现它可以完美地工作.
Further, I followed the instructions in the above link to add the plus
method to the cell
class and find that it works perfectly.
有人可以向我解释如何将这个附加方法添加到table
类中吗?
Can someone please explain to me how I can add this additional method to the table
class?
推荐答案
With the release of Matlab R2012b (version 8), the class folder behavior changed (emphasis is mine):
在MATLAB版本5到7中,类文件夹不会隐藏具有相同名称的其他类文件夹,而是位于更高版本的路径文件夹中.相反,类来自具有相同名称的所有类文件夹中的方法的组合定义了该类. .
为了向后兼容,在类文件夹中定义的类始终优先于具有相同名称的函数和脚本,即使在路径上位于它们之前的那些也是如此.
For backward compatibility, classes defined in class folders always take precedence over functions and scripts having the same name, even those that come before them on the path.
两个粗体语句的组合说明了这种行为:
The combination of the two bold statements explains the behavior:
-
cell
是内置的Matlab函数,该函数早于新的OOP规则,该规则返回其类的实例.在R2012b之前,将方法添加到名为@cell
的类文件夹中,将方法添加到从cell
函数返回的对象中(该对象未使用classdef
或类文件夹定义);保留此功能是为了与旧版用户代码兼容.
在R2012b之后添加了 -
table
,它是通过class文件夹定义的,并且是Sealed
.由于它是Sealed
,因此不能被子类化.有了新规则,没有关联的classdef
文件的任何@table
文件夹都不会注册为类文件夹,也不会将其方法组成现有的类,除非它是旧系统的一部分(例如cell
).
cell
is a built-in Matlab function that predates the new OOP rules that returns an instance of its class. And before R2012b, adding methods to a class folder called@cell
added the methods to the object returned from thecell
function (which isn't defined with aclassdef
nor a class folder); this ability was retained for compatibility with legacy user code.table
was added after R2012b, is defined via a class folder, and isSealed
. Since it isSealed
, it cannot be subclassed. And with the new rules, any@table
folder without an associatedclassdef
file will not register as a class folder nor will its methods be composed into the existing class unless it is part of the legacy system (likecell
).
我可以按照我认为最好的顺序列出三种解决方法:
I can see three workarounds listed in the order I think is best:
- 在Matlab路径上具有一个函数,就像您拥有的那样,并且始终使用函数符号而不是点符号来调用方法".如果需要更多方法",请将它们分组在路径上的文件夹或程序包中(如果需要良好的命名空间).由于
table
是值类,因此此选项似乎不错. -
创建一个包装器类,如下所示.它很麻烦,但是会自动封装其他功能.
- Have a function on the Matlab path, just like the one you have, and always call the "method" using function notation instead of dot notation. If you need more "methods" group them in a folder or package (if good namespacing is desired) on the path. Since
table
is a value class, this option doesn't seem bad. Create a wrapper class like the one below. It is cumbersome but automatically encapsulates the additional functions.
classdef MyTable < handle
properties
tab;
end
methods
function mytab = MyTable(varargin)
mytab.tab = table(varargin{:});
end
function tabnan = nansubset(mytab,r,c)
tabnan = mytab.tab(r,c);
end
end
end
[matlabroot,'\toolbox\matlab\datatypes\@table\*']
的本地副本,然后直接添加方法.我不能想到它本身有什么巨大的缺点,但是复制这样的内部结构感觉很奇怪.
[matlabroot,'\toolbox\matlab\datatypes\@table\*']
and add the methods directly. I can't think of any huge drawbacks to this per se, but it feels weird copying internals like this.这篇关于向Matlab表类添加方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!