|
"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
|