How to modify properties of a Matlab Object

梦想与她 提交于 2019-11-28 05:52:58
Azim

This is similar to this question. In short all you should have to do is inherit from handle class.

Quick example

Contents of file 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

Now from the Matlab command prompt, you can do the following

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

s = 

myclass handle

properties:
    x_array: [5 6]

lists of methods, events, superclasses

There is an easier way. You only need to overwrite your initial instance s as follows:

s= s.x_array

More information here: http://uk.mathworks.com/help/matlab/matlab_oop/matlab-vs-other-oo-languages.html#bslvcv1

PS: While it is fine to use handle, the way copy function works is different and you should be careful about the way you use it. When you use handle, in fact you are making a new address/reference to an obj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!