Suppose I have the following class:
classdef myClass < handle
properties
A = 1
end
methods
function obj = myClass(val)
obj.A = val;
end
end
end
Say I instantiate an instance of this class, then manipulate it slightly and then copy it. As it's a handle class, the "copy" is really just another instance of the same object:
>> q = myClass(10);
>> q.A = 15;
>> w = q;
>> disp(w.A)
15
But I would like to watch A
without needing to instantiate myClass. Naively doing
>> value = w.A
doesn't work, since this just copies the value; changning w.A
later on will not change value
.
Is there a way to provide a "pointer" or "reference" to w.A
without having to create a separate handle class? I'd rather keep the notation w.A
rather than something like w.A.value
(with me having to create the handle class to contain that value).
EDIT: I am using this functionality in order to help encapsulate my code for use with my research lab. I am designing an interface between MATLAB and Arduino to control air and ground vehicles; I was hoping to access stuff like "vehicle.pwmMax
", "vehicle.flightCeiling
", etc, to encapsulate the underlying object: "vehicle.Globals.pwmMax.value
", etc.
I don't think there is anything that will do exactly as you want, given all your constraints.
However, I'm not really clear on your notational issues. Why do you want to retain the notation
w.A
while you are considered aboutvalue
not changing? Keeping the notationw.A
similar is not a real issue.Using some modified code, I can produce following execution:
But there is no way around the notation
value()
as that is the turning point of the implementation; which I think is the closest you can get to what you want. You get the behavior above when you use the following code to implementmyClass
:So you see that the
Aref
method actually returns a function handle which fetches the value from the object. This also means that this reference is read-only!Also note that you will have to instantiate a
myClass
instance before you are able to get the value ofA
(where would you get the value ofA
from otherwise?). This instance does not have to be visible inside your current workspace (e.g. another function scope), since the myClass instance is stored within the function handlevalue
.Drawback of this method is that you only get a read-only reference, you will have to use the call
value()
to get the actual value instead of the function handle (so that changes the notation, but not the one you wanted to keep (or at least it can be made so by substitutingA
in my code byAval
and renamingAref
toA
). Another drawback is that resolvingvalue
might be a bit slower than simply resolving a variable (whether that's a problem will depend on your usage ofvalue()
).If you want some of the notations changed, this can be done by using dependent properties:
The equivalent execution of above is given by:
edit: I thought of another way to implement this, which is simpler (i.e. just keep the class of your original post) but it requires you to change the code in other places. The basic idea behind it is the same as the first ones, but without encapsulating it in the object itself (which makes the object cleaner, IMHO).
Since you are working with a handle class, both
q
andw
in your example refer to the same object in memory; they are themselves a "pointer"/"reference" to the object they represent.So continuing your example, if you make changes to one, it will be reflected in the other.
Also note that you are not creating another instance of the class when you call
w = q;
. Compare the following examples in terms of memory space:Against:
EDIT
Playing around with this, I came up with the following hackish solution.
First we create a wrapper function around the class constructor. It creates an object as usual, plus it returns a function handle that acts as a read-only accessor to a closure variable synced with the original object property using a "PostSet" events listener.
The only change to the original class is to add the
SetObservable
property attribute:myClass.m
myClassWrapper.m
Now we can use the wrapper as:
You could do this by with a PropertyReference class
Continuing your example, you could then use PropertyReference as follows:
Usage of the PropertyReference class is a bit awkward but the original class remains unchanged.
EDIT - Added disp function overload as per strictlyrude27 comment