Calling one method from another method and callback arguments

I have a problem to define callbacks arguments and what matlab documentation says doesn't help me.

My first class:

classdef first < handle
   properties
       State = false
   end
   events
      Contact
   end
   methods
      function OnStateChange(obj,newState)
         if newState ~= obj.State
            obj.State = newState;
            notify(obj,'Contact');
         end
      end
      function follower(obj)
          while 1
              data=inputSingleScan(sessions_daq.NI_USB_1);
              if data>3
                  obj.OnStateChange(true)
              else
                  obj.OnStateChange(false)
              end
          end
      end
   end
end

My second class:

classdef second < handle
   methods
      function obj = answerer(obj_first,obj_third)
         addlistener(obj_first,'Contact',@obj.handleEvnt);
      end

      function handleEvnt(obj,obj_first,~,obj_third,~)
         if obj_first.State
            obj_third.tracker()
         end
      end
   end
end

My third class:

classdef third < handle
    methods
        function tracker(obj)
            %computations  
        end 
    end
end

Here is the error I get:

>> o_first=first;
o_third=third;
o_second=second(o_first,o_third);
>> o_first.follower
Warning: Error occurred while executing callback:
Undefined variable obj_third.

Error in second/handleEvnt (line 9)
            obj_third.tracker()

Error in second>@(varargin)obj.handleEvnt(varargin{:}) (line 4)
         addlistener(obj_first,'Contact',@obj.handleEvnt);

Error in first/OnStateChange (line 12)
            notify(obj,'Contact');

Error in first/follower (line 19)
                  obj.OnStateChange(true)

> In first>first.OnStateChange at 12
  In first>first.follower at 19 

Could you please help me?

Thank you very much

Answers


The issue lies in the way that you are defining your anonymous function as your callback to addlistener. When the event is fired, addlistener will provide your function with two inputs. These inputs are:

  1. The object which fired the event
  2. Some event.EventData object which contains information about the event itself.

So when you specify a callback it must deal with these two inputs appropriately. In your case, you are calling a method of your class, so, you actually pass three inputs to your callback. The first input is the instance of the class and the other two are the ones provided by addlistener.

So in effect, your callback that you're defining is this

@(src, evnt)obj.handleEvnt(src, evnt)

So if you look at your function definition for the method second.handleEvnt, you will see that you have written it to expect five inputs.

function handleEvnt(obj,obj_first,~,obj_third,~)

The immediate reason that you are getting an error is because obj_third and the last input are not passed to this function by the callback that you have provided (since we said that it only passes three inputs).

To fix this you can either re-write your handleEvnt method to accept only three inputs.

function handleEvnt(obj, src, evnt)

Or you can rewrite your anonymous function to pass additional functions to the callback.

@(src,evnt)obj.handleEvnt(src, evnt, other, arguments)

In addition to the methods presented here using anonymous functions, you can also use any of the ways that MATLAB allows callback functions to be defined.


Need Your Help

How to group properties with Spring Boot Configuration Properties

java spring properties configuration spring-boot

According to Spring Boot documentation, properties can be grouped and a property may appear in more than one group. But at the moment when we create a property class marked with @ConfigurationPrope...

iBeacon's customisable UUID conflicts

bluetooth bluetooth-lowenergy ibeacon

If my understanding is correct, there is a large range of UUIDs assigned in the specification - but then there's also this.