Matlab calsses - PreGet listener on structures

2 views (last 30 days)
Florin
Florin on 3 Dec 2015
Commented: Florin on 3 Dec 2015
Hello,
I am using some basic Matlab classes with some dynamic defined properties. I added listeners 'PreGet' and 'PostGet' for a property. My property can be a simple variable or a structure.
This is my class:
classdef myClass < dynamicprops
methods
function obj = myClass()
NewProp = 'myProp';
DynamicProp=addprop(obj,NewProp);
DynamicProp.SetAccess='public';
DynamicProp.GetAccess='public';
DynamicProp.SetObservable=true;
DynamicProp.GetObservable=true;
addlistener(obj , NewProp , 'PreGet' , @obj.PreReadProperty);
%addlistener(obj , NewProp , 'PreSet' , @obj.PreSendProperty);
addlistener(obj , NewProp , 'PostGet' , @obj.PostReadProperty);
%addlistener(obj , NewProp , 'PostSet' , @obj.PostSendProperty);
end
function PreReadProperty(obj, inputMetaProp, varargin)
DynamicProp=obj.findprop((inputMetaProp.Name));
disp(obj.(DynamicProp.Name))
%Body
end
%
function PostReadProperty(obj, inputMetaProp, varargin)
%DynamicProp=obj.findprop(inputMetaProp.Name);
% Body
end
end
end
I am trying to catch in a PreReadProperty the value that I access before displaying it.
Example:
>> myVar = myClass;
myVar.myProp = 1;
mytest = myVar.myProp;
1
>> mytest
mytest =
1
This works fine for a variable. I can catch it and work with it
I am trying to do the same thing when myVar.myProp is a structure:
>> myVar = myClass;
>> myVar.myProp.myProp1 = 1;
>> myVar.myProp.myProp2 = 2;
myProp1: 1
>> mytest = myVar.myProp.myProp1;
myProp1: 1
myProp2: 2
>> mytest
mytest =
1
This doesn't work as expected. I tried to browse inputMetaPropand varargin but I still cant get my read value in the PreReadProperty function
When I read myVar.myProp.myProp1 I want to catch its value in PreReadProperty. Is there any way to do this?
Regards

Answers (1)

Adam
Adam on 3 Dec 2015
You would need something more like:
addlistener(obj.NewProp, 'myProp1', 'PreGet' , @obj.PreReadProperty);
to listen to the actual property that is being set rather than the structure property that contains it. But in your situation that doesn't make sense since you are dealing with a dynamic property and your class does not know about the existence of 'myProp1'.
If you can move away from dynamic properties to static properties then you could do this, but otherwise what you are effectively trying to do is have your class listen for a change to a property it doesn't even know exists.
  1 Comment
Florin
Florin on 3 Dec 2015
Hello,
Unfortunately this doesn't work in my case. I need to use dynamic properties and the structure tree does not stop at 1...2
Listening for a change is not so difficult. Listening for a call is. I am trying to catch my value while executing the 'PreReadProperty' function.
In debug mode (somewhere in 'PreReadProperty' function) if I use twice 'dbup' (go to Base) I get the correct value in the variable 'ans'
Regards

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!