2008-11-07 73 views
21

我创建了一个MATLAB类Matlab的对象的属性,喜欢的东西:如何修改

classdef myclass 

    properties 
     x_array = []; 
    end 

    methods 
    function increment(obj,value) 
     obj.x_array = [obj.x_array ; value); 
    end 
    end 
end 

问题是,物业x_array永远不会当我调用increment()功能修改: 例如:

>>s = myclass 
>>increment(s,5) 

>>s.x_array 
ans = [] 

我做了一些研究,我得出了一个结论,这是因为使用MATLAB懒惰复制的对象,使我的类继承手柄类应该已经解决了这个,但它没有,没有任何人知道为什么这是发生宁?如果扩展处理类是indeen解决方案,是不是这样做的正确方法:

classdef myclass < handle 

或是否有任何额外的步骤?

+0

几乎是一个重复到http://stackoverflow.com/questions/209005/object-oriented-matlab-properties – Azim 2008-11-07 17:50:14

回答

22

这与this question类似。总之你所要做的就是继承handle类。

简单的例子

文件myclass.m

classdef myclass<handle 
    properties 
     x_array = [] 
    end 
    methods 
     function obj=increment(obj,val) 
      obj.x_array=[obj.x_array val]; 
     end 
    end 
end 

现在从MATLAB命令提示符的内容,你可以做以下

>> s=myclass; 
>> s.increment(5) 
>> s.increment(6) 
>> s 

s = 

myclass handle 

properties: 
    x_array: [5 6] 

lists of methods, events, superclasses