如何修改Matlab对象的属性
我创建了一个MATLAB类,类似于:如何修改Matlab对象的属性,matlab,properties,matlab-class,Matlab,Properties,Matlab Class,我创建了一个MATLAB类,类似于: classdef myclass properties x_array = []; end methods function increment(obj,value) obj.x_array = [obj.x_array ; value); end end end 问题是,当我调用increment()函数时,属性x\u array从未被修改: 例: 我做了一些研究,得出了一个结论,这是因为MA
classdef myclass
properties
x_array = [];
end
methods
function increment(obj,value)
obj.x_array = [obj.x_array ; value);
end
end
end
问题是,当我调用increment()
函数时,属性x\u array
从未被修改:
例:
我做了一些研究,得出了一个结论,这是因为MATLAB对对象使用惰性复制,让我的类继承句柄类应该解决这个问题,但它没有,有人知道为什么会发生这种情况吗?如果在解决方案中扩展handle类,这不是正确的方法吗:
classdef myclass < handle
classdef myclass
或者是否有额外的步骤?这类似于。简而言之,您需要做的就是从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
有一个更简单的方法。您只需覆盖初始实例
s
,如下所示:
s = increment(s,5);
更多信息
PS:虽然可以使用handle,但复制功能的工作方式是不同的,您应该小心使用它。当您使用handle时,实际上您正在对obj进行一个新的地址/引用以进行比较,handle类构造函数充当传统的python类?
>> s=myclass;
>> s.increment(5)
>> s.increment(6)
>> s
s =
myclass handle
properties:
x_array: [5 6]
lists of methods, events, superclasses
s = increment(s,5);