MATLAB类对象未更新 [英] MATLAB Class Object Not Updated

查看:122
本文介绍了MATLAB类对象未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的MATLAB类,它有几个属性和一个方法。类的构造函数使用默认值初始化属性。类的方法在构造类之后获取一个附加输入,以便更新类属性。

I am writing a simple MATLAB class that has a few properties and a method. The constructor of the class initializes the properties with the default values. The method of the class gets an additional input after the class is constructed in order to update the class properties.

classdef classTest
    properties
        p1
        p2
        p3
        p4
    end

    methods

        function obj = classTest()
            obj.p1 = 0;
            obj.p2 = 0;
            obj.p3 = [];
            obj.p4 = '';
        end

        function obj = updateSomeProperties( obj, p1 )
            obj.p1 = p1;
        end
    end

end

当我调用类的方法时,它不更新属性。

However, when I call the method of the class it does not update the properties.

>> b = classTest

b = 

  classTest with properties:

    p1: 0
    p2: 0
    p3: []
    p4: ''

>> b.updateSomeProperties(10)

ans = 

  classTest with properties:

    p1: 10
    p2: 0
    p3: []
    p4: ''

>> b % still used the default values.

b = 

  classTest with properties:

    p1: 0
    p2: 0
    p3: []
    p4: ''



我想知道为什么在调用类的方法之后, b 未更新,但 updateSomeProperties 更新类对象。

I wanted to know why after calling the method of the class, b is not updated although updateSomeProperties updates the class object.

推荐答案

问题是你的类是一个 value 类,它被传递(即使它自己的方法)作为副本。这是MATLAB类的默认行为,因为这是所有基本的MATLAB数据类型都是。我们可以通过查看调用 updateSomeProperties()的输出来验证这种情况。您将看到返回的结果(显示为 ans )包含您期望的修改,但这些更改不存在于原始对象中, b 。如果你想坚持一个值类,你需要从方法中返回新的对象,并在调用方法时重新分配变量。

The issue is that your class is a value class which is passed (even to it's own methods) as a copy. This is the default behavior for MATLAB classes as this is what all fundamental MATLAB datatypes are. We can verify that this is the case by looking at the output of your call to updateSomeProperties(). You will see that the returned result (shown as ans) contains the modification that you expect but these changes are not present in your original object, b. If you wanted to stick with a value class, you would need to return the new object from the method and re-assign the variable when calling the method.

b = classTest();
b = b.updateSomeProperties(10);

你想要的是一个句柄类,总是通过引用传递。这允许一种方法在相同对象上操作,而不是修改原始对象的副本。

What you want is a handle class which is always passed by reference. This allows a method to operate on the same object rather than modifying a copy of the original object.

要执行此操作,您需要继承内置的 handle 类。

To do this, you will want to inherit from the built-in handle class.

classdef classTest < handle

有句柄和值类的详细比较

There is a detailed comparison of handle and value classes in the documentation.

作为旁注,不是手动设置构造函数中的所有默认属性值,而是可以在属性块本身中简单地指定这些默认值。

As a side-note, rather than manually setting all default property values in the constructor, it is possible to simply specify these defaults within the properties block itself.

properties
    p1 = 0;
    p2 = 0;
    p3 = [];
    p4 = '';
end

这篇关于MATLAB类对象未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆