向Matlab表类添加方法 [英] Add a method to Matlab table class

查看:104
本文介绍了向Matlab表类添加方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在table类中添加一个名为nansubset的方法.本质上,它允许您调用T(r,c),其中rc是可能包含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?

推荐答案

随着Matlab R2012b(版本8)的发布,

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 the cell function (which isn't defined with a classdef 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 is Sealed. Since it is Sealed, it cannot be subclassed. And with the new rules, any @table folder without an associated classdef 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 (like cell).

我可以按照我认为最好的顺序列出三种解决方法:

I can see three workarounds listed in the order I think is best:

  1. 在Matlab路径上具有一个函数,就像您拥有的那样,并且始终使用函数符号而不是点符号来调用方法".如果需要更多方法",请将它们分组在路径上的文件夹或程序包中(如果需要良好的命名空间).由于table是值类,因此此选项似乎不错.
  2. 创建一个包装器类,如下所示.它很麻烦,但是会自动封装其他功能.

  1. 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.
  2. 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\*']的本地副本,然后直接添加方法.我不能想到它本身有什么巨大的缺点,但是复制这样的内部结构感觉很奇怪.
  • Create a local copy of [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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆