2

有一个句柄类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

我想创建一个指向. 这在 Matlab 中可能吗?例如,以下内容不起作用:afoo.x

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.
4

1 回答 1

1

不,不幸的是,这在 Matlab 中是不可能的。引用只能指向作为从句柄继承的某些类的实例的对象(例如您的 class Foo)。即,这是可能的:

bar = foo
foo.x = [3 4]
disp(bar.x)      % would be [3 4]
于 2013-02-14T16:03:51.343 回答