Alexey
Alexey

Reputation: 5978

Matlab -- handle objects

There is a handle class Foo:

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

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

I create an object of Foo:

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

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.

Upvotes: 2

Views: 1314

Answers (1)

miy
miy

Reputation: 306

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]

Upvotes: 1

Related Questions