Matlab-处理对象 [英] Matlab -- handle objects

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

问题描述

有一个句柄类Foo:

classdef Foo < handle   
    properties (SetAccess = public, GetAccess = public)
        x
    end

    methods
        function obj = foo(x)
            % constructor
            obj.x = x;
        end 
    end       
end

我创建Foo的对象:

data = [1 2];
foo = Foo(data);  % handle object

我想创建一个指向foo.x的引用( handle )变量a.在Matlab中有可能吗?例如,以下方法不起作用:

I would like to create a reference (handle) variable a that points to foo.x. Is this possible in Matlab? For example, the following does not work:

a = foo.x;       % here `a` equals [1 2], `a` is not a handle object  
foo.x = [3 4];   % set property `x` to [3 4];
disp(a);         % a still equals [1 2] 
                 % even though the property `foo.x` has changed
                 % I want `a` to change when `foo.x` is changed.

推荐答案

否,不幸的是,在Matlab中是不可能的.引用只能是作为从handle继承的某个类的实例的对象(例如您的类Foo).也就是说,这是可能的:

No, unfortunately it is not possible in Matlab. References can be only to objects which are instances of some class inherited from handle (like your class Foo). I.e., this is possible:

bar = foo
foo.x = [3 4]
disp(bar.x)      % would be [3 4]

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

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