Arrays 类数组中的方法

Arrays 类数组中的方法,arrays,matlab,class,Arrays,Matlab,Class,我有几个类,有很多方法和函数,它们通常处理mx1数组。对于给定的类: S1.x=randn(m,1); S1+2*randn(m,1); % Plus Override S1.smooth; % Class Methods S1.detrend; 现在,我希望以这样的方式处理相同给定类的数组 S=[S1 S2 S3 S4]; S.smooth; % Methods for Class Array S.detrend; 问题: 是否有一种简单的方法可以实现这一点,而无需重写所有实现类属性和方

我有几个
,有很多
方法
函数
,它们通常处理
mx1
数组
。对于给定的

S1.x=randn(m,1);
S1+2*randn(m,1); % Plus Override
S1.smooth; % Class Methods
S1.detrend; 
现在,我希望以这样的方式处理相同给定
类的
数组

S=[S1 S2 S3 S4];
S.smooth; % Methods for Class Array
S.detrend;
问题是否有一种简单的方法可以实现这一点,而无需重写所有实现
属性和
方法的
函数

我正在寻找一些具体的补充,重新定义,一段代码,技巧等,以做到这一点在一个干净的方式。这样做的目的是代码功能,而不是性能-少数性能关键的功能已经矢量化了

您好,

怎么样:

classdef TestClass
   methods
       function smooth(obj)
           if numel(obj) == 1
               disp('Hello')
           else
               for i=1:numel(obj)
                   obj(i).smooth;
               end
           end

       end
   end
end
其名称如下:

>> t1 = TestClass;
>> t1.smooth
Hello


>> t2 = [TestClass TestClass TestClass];    
>> t2.smooth
Hello
Hello
Hello
如果您真的想这样做,您还应该能够重载操作符,以便在所有方法上自动执行该操作(它允许您访问
操作符)。然而,根据我的经验,正确地重载subsref并不是直截了当的,可能需要付出比实际更大的努力

下面是这个想法的一个例子。这太简单了,你可能需要进一步的改进,但是应该开始了。注意黑客的数量:)


感谢您提供的函数示例代码。因为我有这么多这样的函数,所以我不惜一切代价避免通过
for
循环加测试条件对所有函数进行修改。我仍然不清楚如何重载
subsref
操作符或任何其他操作符来实现这一点。虽然这是一个很大的暗示。是的,它似乎一点也不直截了当。。。。但这是一种解决方案,它将保持代码的其余部分完好无损,或者只需稍加修改…@hypfco我已经更新了解决方案,希望能有所帮助。当方法返回一个值、多个值、无值、具有可变的输出数、访问属性、正确处理公共/私有方法等时,您需要考虑您想要做什么。无论如何,我认为这应该给您足够的时间来使用:)谢谢。这就是我一直在寻找的解决方案。。。。我将检查操作并将问题标记为已解决。关于输出,我担心它会更简单,因为函数没有输出…
S1、S2、S3、S4是不同类的实例,还是相同类的实例?是的。班级是一样的。
classdef TestClass
properties
    Value
end

methods        
    function obj = TestClass(x)
        obj.Value = x;
    end

    function smooth(obj)
        fprintf('I am %d\n', obj.Value)
    end

    function res = opposite(obj)
        res = -obj.Value;
    end

    function [res1,res2,res3] = test(obj)
        res1 = obj.Value;
        res2 = res1*res1;
        res3 = res2*res1;
    end

    function varargout = subsref(A,S)
        if numel(A) > 1 && strcmp(S(1).type, '.')
            if nargout == 0
                feval(S.subs, A(1));
            else
                nout = nargout(['TestClass>TestClass.' S.subs]);
                if nout < 0
                    nout = -nout;
                end
                if nout == 0
                    arrayfun(@(x)feval(S.subs, x), A);
                    varargout = cell(1, nargout);
                else
                    for i=1:nargout                            
                        [output{1:nout}] = feval(S.subs, A(i));
                        varargout{i} = output;
                        output = {};
                    end
                end

            end
        else
            if nargout == 0
                builtin('subsref', A, S);
            else
                varargout{:} = builtin('subsref', A, S);
            end
        end
    end        
end
>> t1 = TestClass(5);
>> t1.smooth;
I am 5

>> t2 = [TestClass(1) TestClass(2) TestClass(3)];
>> t2(2).smooth;
I am 2
>> t2.smooth;
I am 1
I am 2
I am 3
>> t2(1:2).smooth
I am 1
I am 2
>> t2(2:3).smooth
I am 2
I am 3
>> t2([1 3]).smooth
I am 1
I am 3
>> t2.test

ans = 

    [1]    [1]    [1]


ans = 

    [2]    [4]    [8]


ans = 

    [3]    [9]    [27]