Clear Filters
Clear Filters

Listening an event when another function of the class is running?

9 views (last 30 days)
Hello,
My class looks like:
classdef linescan99 < handle
methods
function obj = linescan99(obj_surveilleur_de_contact)
addlistener(obj_surveilleur_de_contact,'Contact',@obj.handleEvnt);
end
function follower(obj)
while 1
%computations
end
end
end
end
I've noted that when the follower function is executing, the class is unable to listen a possible event 'Contact'. What's unfortunate: this event would lead to a break of the follower function while 1 loop...
Do you know how to force the function to listen the event? maybe inside the loop?
Thank you very much

Accepted Answer

Guillaume
Guillaume on 17 Mar 2016
I think the problem is that your function suiveur is called by the event handler, so your while loop is executing while an event is already being handled. I'll be very surprised if event handlers are reentrant in matlab, so no new event can be handled until you've finished dealing with the current one (in effect until you've quit your loop).
You're trying to do some multithreaded (or asynchronous) programming here which is not really supported by matlab. A possible workaround would be to use a timer to periodically check whether or not the event has been raised:
%Warning: completely untested code. There may be typos:
classdef linescan99 < handle
properties (SetAccess = private)
suiveur_en_cour = false;
minuteur;
tracking = false;
end
methods
function this = linescan99(obj_surveilleur_de_contact) %constructor
addlistener(obj_surveilleur_de_contact,'Contact',@obj.handleEvnt);
this.minuteur = timer('BusyMode', 'drop', 'ExecutionMode', 'fixedRate', 'Period', 0.1, 'TimerFcn', @obj.suiveur);
this.minuteur.start();
end
function handleEvnt(this, obj_surveilleur_de_contact,~)
if obj_surveilleur_de_contact.State %contact
disp('linescan lancé')
end
this.suiveur_en_cour = obj_surveilleur_de_contact.State;
end
function suiveur(this, ~, ~)
if suiveur_en_cour
if ~tracking %this is just to make sure disp is only called once, when tracking begins
tracking = true;
disp('tracking en cours');
end
%do whatever you wanted to do in your while loop
else
tracking = false;
end
end
end
end
If this works for you, you could also simply store a reference to obj_surveilleur_de_contact in linescan99, check its state with the timer and not bother with the listener or events.
  2 Comments
Michaël
Michaël on 17 Mar 2016
Thank you very much!
Which "ideal" language are you thinking of for this kind of programming?
Guillaume
Guillaume on 18 Mar 2016
With the design you have, what you want is to launch a background or asynchronous task from the event handler, so that the handler can return immediately.
Matlab parallel processing toolbox would probably let you do that easily but since I don't have and have never used that toolbox, I can't help you with that.
The 'ideal' language is the one you know, really. Among the ones I know, asynchronous programming is very easy with .Net (so C#, VB.Net or C++/.net) .

Sign in to comment.

More Answers (2)

Geoff Hayes
Geoff Hayes on 17 Mar 2016
Michaël - without seeing more of your code, in particular the handleEvent or all of follower, it is difficult to know for sure what is going wrong. But if I were to guess, it may be because of your while loop
while 1
% computations
end
Is this a "tight" while loop that does not allow itself to be interrupted? Are there any calls to drawnow or pause? If not, then this could mean that the while loop will continue to run and monopolize all the processing thus not "giving a chance" to any other call until it has been completed. If I ever write something similar, then I put a short pause in the code to temporarily halt execution and allow other events (callbacks etc.) to occur.
while 1
% computations
% pause for fraction of a second
pause(0.01);
end
Try the above and see what happens!
  5 Comments
Geoff Hayes
Geoff Hayes on 17 Mar 2016
...the while 1 of my first class doesn't seem to be executed during this pause
Isn't the pause in the while loop? Can you post the code (i.e. attach) so that we can see the changes?

Sign in to comment.


ARPIT
ARPIT on 7 Jun 2017
Hey Michael,
Can you kindly suggest how you finally implemented it because I also want to do exactly similar thing.
Thanks in advance
  1 Comment
Michaël E
Michaël E on 8 Jun 2017
Hi,
Matlab isn't able to run concomitantly two (while) loops; a workaround is to use the "timer" object.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!