2

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.

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Yes, that's a big limitation of Matlab, unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.