MatLab OOP不改变属性

时间:2015-05-10 22:42:24

标签: matlab oop

我有一个matlab类,其中包含属性' next state'。

我在类中定义了以下函数:

function obj = decideNextAction(obj)
            obj.current_patch_quality
            % Important, rand is redeclared in the two calls. So it may be
            % that rand < current_patch_quality in the first if and greater
            % than in the second if
            if(rand > obj.current_patch_quality)
                obj.next_action = 1;
            elseif(rand < obj.current_patch_quality)
                obj.next_action = 3;
            else
                obj.next_action = 2;
            end   


        end

哪个应该将实例属性重新定义为1,2或3.但是,我做了一些试验,看起来该函数返回带有修改实例的新对象,但不修改原始对象。有什么建议吗?

>> x = recruit([0 0])

x =

  recruit with properties:

             nest_location: [0 0]
    current_patch_location: []
     current_patch_quality: 0
               next_action: []

>> x.decideNextAction

ans =

     0


ans =

  recruit with properties:

             nest_location: [0 0]
    current_patch_location: []
     current_patch_quality: 0
               next_action: 1

>> x

x =

  recruit with properties:

             nest_location: [0 0]
    current_patch_location: []
     current_patch_quality: 0
               next_action: []

>>

1 个答案:

答案 0 :(得分:3)

您必须在handle class中使用classdef

classdef myclass < handle

如果您使用value class,则必须是:

x = x.decideNextAction();