Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
A question about callbacks in matlab

Subject: A question about callbacks in matlab

From: Corbin Holland

Date: 30 Sep, 2010 19:06:19

Message: 1 of 9

Anyone know of a way to get matlab to issue a callback when a line's Parent property is changed to a different axes?

Subject: A question about callbacks in matlab

From: Matt Fig

Date: 30 Sep, 2010 20:09:04

Message: 2 of 9

I can't think of one, but if you are issuing a command to change the line's parent, why not just call some function after that command?

Subject: A question about callbacks in matlab

From: Corbin Holland

Date: 1 Oct, 2010 02:29:06

Message: 3 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <i82qp0$t0k$1@fred.mathworks.com>...
> I can't think of one, but if you are issuing a command to change the line's parent, why not just call some function after that command?

Because I am not changing the line's parent myself, one of my matlab user colleagues does. I just want to detect when they add a line to an axes and then run some code behind the scenes. Sort of like an axes listener that just listens for the children property to change and execute a bit of code when it does.

Subject: A question about callbacks in matlab

From: Matt Fig

Date: 1 Oct, 2010 04:48:05

Message: 4 of 9

Calling Yair Altman.

Subject: A question about callbacks in matlab

From: Walter Roberson

Date: 1 Oct, 2010 05:50:15

Message: 5 of 9

On 30/09/10 2:06 PM, Corbin Holland wrote:
> Anyone know of a way to get matlab to issue a callback when a line's
> Parent property is changed to a different axes?

I suggest examining the implementation of linkprop() . It will not
itself do what you want, but it shows how to add listeners to intercept
changes to hg properties.

Subject: A question about callbacks in matlab

From: Yair Altman

Date: 1 Oct, 2010 09:27:05

Message: 6 of 9

Walter Roberson <roberson@hushmail.com> wrote in message <sEepo.11529$sB4.3019@newsfe15.iad>...
> On 30/09/10 2:06 PM, Corbin Holland wrote:
> > Anyone know of a way to get matlab to issue a callback when a line's
> > Parent property is changed to a different axes?
>
> I suggest examining the implementation of linkprop() . It will not
> itself do what you want, but it shows how to add listeners to intercept
> changes to hg properties.

The technical method of setting callback listeners on HG properties (on which linkprop and many other Matlab functions depend) is to use the undocumented handle.listener built-in function. There are a couple of ways this function can be used, explained here: http://undocumentedmatlab.com/blog/continuous-slider-callback/

Handle listeners are so often used in internal Matlab code that although they are undocumented, it is hard to believe that they will be discontinued anytime soon. However, the function's interface may indeed change somewhat, as has happened a few years ago (when "PreSet" became "PropertyPreSet" etc. sometime around R2007).

Some other posts that describe uses of handle listeners: http://undocumentedmatlab.com/blog/category/listeners/

Note that there are several limitations and provisos with handle.listeners, but as long as you stick to normal usage, as I believe your case is, then it should work very well.

Yair Altman
http://UndocumentedMatlab.com

Subject: A question about callbacks in matlab

From: Corbin Holland

Date: 1 Oct, 2010 19:10:24

Message: 7 of 9

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <i849h9$5rq$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <sEepo.11529$sB4.3019@newsfe15.iad>...
> > On 30/09/10 2:06 PM, Corbin Holland wrote:
> > > Anyone know of a way to get matlab to issue a callback when a line's
> > > Parent property is changed to a different axes?
> >
> > I suggest examining the implementation of linkprop() . It will not
> > itself do what you want, but it shows how to add listeners to intercept
> > changes to hg properties.
>
> The technical method of setting callback listeners on HG properties (on which linkprop and many other Matlab functions depend) is to use the undocumented handle.listener built-in function. There are a couple of ways this function can be used, explained here: http://undocumentedmatlab.com/blog/continuous-slider-callback/
>
> Handle listeners are so often used in internal Matlab code that although they are undocumented, it is hard to believe that they will be discontinued anytime soon. However, the function's interface may indeed change somewhat, as has happened a few years ago (when "PreSet" became "PropertyPreSet" etc. sometime around R2007).
>
> Some other posts that describe uses of handle listeners: http://undocumentedmatlab.com/blog/category/listeners/
>
> Note that there are several limitations and provisos with handle.listeners, but as long as you stick to normal usage, as I believe your case is, then it should work very well.
>
> Yair Altman
> http://UndocumentedMatlab.com

Thank you so much for posting as I am hopeful this will work for me :) One problem though. I wrote pre and post prop change listeners for an axes 'Color' prop and 'Children' prop. The 'Color' one works perfectly. The 'Children' one does not. I can't quite figure out what is going on. Here is the code. What am I doing wrong?

function hax = matlabAxesPropChange

hax = axes('Color',[.5,.3,.2]);
hhax = handle(hax);

hProp = findprop(hhax,'Color'); % a schema.prop object
preListener = handle.listener(hhax,hProp,'PropertyPreSet',@BeforeColorChange);
postListener = handle.listener(hhax,hProp,'PropertyPostSet',@AfterColorChange);

setappdata(hax,'colorListeners',[preListener postListener]);

hProp = findprop(hhax,'Children'); % a schema.prop object
preListener = handle.listener(hhax,hProp,'PropertyPreSet',@BeforeChildChange);
postListener = handle.listener(hhax,hProp,'PropertyPostSet',@AfterChildChange);

setappdata(hax,'childListeners',[preListener postListener]);

% Test Color Prop Change Listener (works)
set(hax,'Color',[1,1,1]);

% Test Children Prop Change Listener (doesn't work)
x = 0:pi:100*pi;
y = sin(x);
hline1 = line('XData',x,'YData',y,'Color','blue','LineWidth',5);
hline2 = line('XData',x*2,'YData',y*2,'Color','red');
set([hline1,hline2],'Parent',hax); % does not trigger prop change event!!

% Swap the position of the lines to prove we made a change
children = get(hax,'children');
temp = children(1);
children(1) = children(2);
children(2) = temp;

set(hax,'Children',children); % also does not trigger prop change event!!


end

%%
function BeforeColorChange( src, evt )

   disp('====== Color Prop PreChange Callback ======');
   hax = get(evt,'AffectedObject');
   oldColor = get(hax,'Color');
   fprintf(sprintf('Old Color: %s\n',arr2str(oldColor)));
   
end

%%
function AfterColorChange( src, evt )

   disp('====== Color Prop PostChange Callback ======');
   hax = get(evt,'AffectedObject');
   newColor = get(hax,'Color');
   fprintf(sprintf('New Color: %s\n',arr2str(newColor)));
   
end

%%
function BeforeChildChange( src, evt )

   disp('====== Child Prop PreChange Callback ======');
   hax = get(evt,'AffectedObject');
   oldChildren = get(hax,'Children');
   fprintf(sprintf('Old Child Count: %s\n',length(oldChildren)));
   
end

%%
function AfterChildChange( src, evt )

   disp('====== Child Prop PostChange Callback ======');
   hax = get(evt,'AffectedObject');
   newChildren = get(hax,'Children');
   fprintf(sprintf('New Child Count: %s\n',length(newChildren)));
   
end

Subject: A question about callbacks in matlab

From: Donn Shull

Date: 1 Dec, 2010 20:27:05

Message: 8 of 9

"Corbin Holland" <cholland.nospam@opticalsciences.com> wrote in message <i85bn0$ios$1@fred.mathworks.com>...
> "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <i849h9$5rq$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <sEepo.11529$sB4.3019@newsfe15.iad>...
> > > On 30/09/10 2:06 PM, Corbin Holland wrote:
> > > > Anyone know of a way to get matlab to issue a callback when a line's
> > > > Parent property is changed to a different axes?
> > >
> > > I suggest examining the implementation of linkprop() . It will not
> > > itself do what you want, but it shows how to add listeners to intercept
> > > changes to hg properties.
> >
> > The technical method of setting callback listeners on HG properties (on which linkprop and many other Matlab functions depend) is to use the undocumented handle.listener built-in function. There are a couple of ways this function can be used, explained here: http://undocumentedmatlab.com/blog/continuous-slider-callback/
> >
> > Handle listeners are so often used in internal Matlab code that although they are undocumented, it is hard to believe that they will be discontinued anytime soon. However, the function's interface may indeed change somewhat, as has happened a few years ago (when "PreSet" became "PropertyPreSet" etc. sometime around R2007).
> >
> > Some other posts that describe uses of handle listeners: http://undocumentedmatlab.com/blog/category/listeners/
> >
> > Note that there are several limitations and provisos with handle.listeners, but as long as you stick to normal usage, as I believe your case is, then it should work very well.
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
>
> Thank you so much for posting as I am hopeful this will work for me :) One problem though. I wrote pre and post prop change listeners for an axes 'Color' prop and 'Children' prop. The 'Color' one works perfectly. The 'Children' one does not. I can't quite figure out what is going on. Here is the code. What am I doing wrong?
>
> function hax = matlabAxesPropChange
>
> hax = axes('Color',[.5,.3,.2]);
> hhax = handle(hax);
>
> hProp = findprop(hhax,'Color'); % a schema.prop object
> preListener = handle.listener(hhax,hProp,'PropertyPreSet',@BeforeColorChange);
> postListener = handle.listener(hhax,hProp,'PropertyPostSet',@AfterColorChange);
>
> setappdata(hax,'colorListeners',[preListener postListener]);
>
> hProp = findprop(hhax,'Children'); % a schema.prop object
> preListener = handle.listener(hhax,hProp,'PropertyPreSet',@BeforeChildChange);
> postListener = handle.listener(hhax,hProp,'PropertyPostSet',@AfterChildChange);
>
> setappdata(hax,'childListeners',[preListener postListener]);
>
> % Test Color Prop Change Listener (works)
> set(hax,'Color',[1,1,1]);
>
> % Test Children Prop Change Listener (doesn't work)
> x = 0:pi:100*pi;
> y = sin(x);
> hline1 = line('XData',x,'YData',y,'Color','blue','LineWidth',5);
> hline2 = line('XData',x*2,'YData',y*2,'Color','red');
> set([hline1,hline2],'Parent',hax); % does not trigger prop change event!!
>
> % Swap the position of the lines to prove we made a change
> children = get(hax,'children');
> temp = children(1);
> children(1) = children(2);
> children(2) = temp;
>
> set(hax,'Children',children); % also does not trigger prop change event!!
>
>
> end
>
> %%
> function BeforeColorChange( src, evt )
>
> disp('====== Color Prop PreChange Callback ======');
> hax = get(evt,'AffectedObject');
> oldColor = get(hax,'Color');
> fprintf(sprintf('Old Color: %s\n',arr2str(oldColor)));
>
> end
>
> %%
> function AfterColorChange( src, evt )
>
> disp('====== Color Prop PostChange Callback ======');
> hax = get(evt,'AffectedObject');
> newColor = get(hax,'Color');
> fprintf(sprintf('New Color: %s\n',arr2str(newColor)));
>
> end
>
> %%
> function BeforeChildChange( src, evt )
>
> disp('====== Child Prop PreChange Callback ======');
> hax = get(evt,'AffectedObject');
> oldChildren = get(hax,'Children');
> fprintf(sprintf('Old Child Count: %s\n',length(oldChildren)));
>
> end
>
> %%
> function AfterChildChange( src, evt )
>
> disp('====== Child Prop PostChange Callback ======');
> hax = get(evt,'AffectedObject');
> newChildren = get(hax,'Children');
> fprintf(sprintf('New Child Count: %s\n',length(newChildren)));
>
> end

Hi Corbin,

The problem you are seeing is because the Chuldren property is "special". It reflects the handle graphics tree structure built using the hg connect mechanism. In spite of how it looks you really cannot change it with set and get does not really access it. Because of this the property events you are monitoring are never generated. What you can do instead is use the axes object tree events. Yair has a post planned which will explain this better. For the axes object there are two events you can use 'ObjectChildAdded' and ObjectChildRemoved'. The listener objects you would use would be

addedListener = handle.listener(hhax,'ObjectChildAdded',@ChildAdded);
removedListener = handle.listener(hhax,'ObjectChildRemoved',@ChildRemoved);

function ChildAdded( src, evt )

   disp('====== Child Added Callback ======');
   hax = get(evt,'Source');
   oldChildren = get(hax,'Children');
   fprintf(sprintf('Child Count: %s\n',num2str(length(oldChildren))));
   
end

function ChildRemoved( src, evt )

   disp('====== Child Removed Callback ======');
   hax = get(evt,'Source');
   oldChildren = get(hax,'Children');
   fprintf(sprintf('Child Count: %s\n',num2str(length(oldChildren))));
   
end

Hope This Helps,

Donn

Subject: A question about callbacks in matlab

From: Ed Ross

Date: 18 May, 2011 20:39:04

Message: 9 of 9

Reviving this post for a bit more clarification...

I have successfully used the ObjectChildRemoved event to detect a line deleted froman axes. I understand how to get the axes children before the child removal in the ObjectChildRemoved event handler but how do I get the updated list of the children handles after the child has been removed? Or, how do I determine the handle of the removed child?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us