Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Static Callback Methods?
Replies: 6   Last Post: Aug 5, 2010 5:01 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
per isakson

Posts: 301
Registered: 9/13/07
Re: Static Callback Methods?
Posted: Aug 3, 2010 11:01 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

JoeyB <joseph.burgel@gm.com> wrote in message <b48aa8df-ed2d-4a6a-8f06-d68846ffa013@z10g2000yqb.googlegroups.com>...
> Ok, Long time ML coder, first time ML OO coder. After about 3 hours of
> messing around with Matlab's OO syntax, I've discovered that any
> callback function being assigned as an event listener must be declared
> as a static method? Where in the doc's does it talk about this? Why?
> Here's the Matlab help example classes:
>
> The Event Creator:
>
> classdef ToggleButton < handle
> properties
> State = false
> end
> events
> ToggledState
> end
> methods
> ...
> function OnStateChange(obj,newState)
> % Call this method to check for state change
> if newState ~= obj.State
> obj.State = newState;
> notify(obj,'ToggledState'); % Broadcast notice of event
> end
> end
> end
> end
>
> And the Event Consumer:
>
> classdef RespondToToggle < handle
> methods
> function obj = RespondToToggle(toggle_button_obj)
>
> addlistener(toggle_button_obj,'ToggledState',@RespondToToggle.handleEvnt);
> end
> end
> methods (Static)
> function handleEvnt(src,evtdata)
> disp(nargin)
> if src.State
> disp('ToggledState is true') % Respond to true
> ToggleState here
> else
> disp('ToggledState is false') % Respond to false
> ToggleState here
> end
> end
> end
> end
>
>
> Then, by typing the following on the command line:
>

> >> clear all
> >> tb = ToggleButton;
> >> rtt = RespondToToggle(tb)
> >> tb.OnStateChage(true)

>
> You get:
>
> ToggledState is true
>
> Great. No problem. BUT...if you change the callback's method attribute
> in RespondToToggle so that it's not static:
> ...
>
> methods
> function handleEvnt(src,evtdata)
> disp(nargin)
> if src.State
> disp('ToggledState is true') % Respond to true
> ToggleState here
> else
> disp('ToggledState is false') % Respond to false
> ToggleState here
> end
> end
> end
> ...
>
> You get:
>
> tb.OnStateChange(true)
> Warning: Error occurred while executing callback:
> Undefined function or method 'RespondToToggle.handleEvnt'
> for input arguments of type 'ToggleButton'.
>
> What doc did I miss where this is described? Why do I need to do
> this? Also, don't believe that 'clear' will reset you workspace by
> believing this warning:
> What's with these nebulous warnings? The doc seems to go over the same
> trivial description of use about 15 different ways but doesn't
> describe what you need
> to know to make this stuff work.
>
> Warning: The class file for 'RespondToToggle' has been
> changed; but the change cannot be applied because objects
> based on the old class file still exist. If you use those
> objects, you might get unexpected results. You can use
> the 'clear' command to remove those objects. See 'help
> clear' for information on how to remove those objects.
>
> You need to type 'clear all' for the workspace to recognize your
> changes.
>
> Just needed to vent. Any comments welcome.


I made may first serious experiments with event the other day. Here is a line from one of the examples in the Matlab OOP book.

addlistener( this.FcnObject, 'Lm', ...
'PostSet', @( src, evnt ) listenLm( this, src, evnt ) );

listenLm is an ordinary method (-not static). My "guess" is that the callback is invoked in the base workspace. Thus, your example works with the static method. In "my" case the "scope of the object" is stored in the anynonous function.

IMO this matter could have been better described in the documentation.

/ per



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.