How to disable call back till I am done with Call back function

2 views (last 30 days)
I have a call back function for capturing mouse moment.
set(gcf, 'WindowButtonMotionFcn', {@myCallBack, handles});
Inside the call back function I do some calculations.
function myCallBack(varargin)
hObject = varargin{1};
mpos = get(hObject,'CurrentPoint');
x = mpos(1);
y = mpos(2);
%perform a complex calculation
varargin{3}.robot.MoveRobotxy(x,y);
The calculation performing is time intensive. Therefore when i run the code, callback function stop working after few seconds. Same thing happens when I put a 2 second pause instead calculations inside the call back.
pause(2)
How do I avoid this? Is there a way to disable the call back till I am done with processing everything inside call back function?
Thank you
  1 Comment
Stephen23
Stephen23 on 1 Nov 2017
It is not clear what you want to achieve: once the callback has been triggered, it will run... and that will take how ever long the code will take. When exactly would you want to "disable" the callback? Are you trying to prevent it from being triggered again, once it is running?
This might be of interest:

Sign in to comment.

Accepted Answer

Jos (10584)
Jos (10584) on 1 Nov 2017
You can use a persistent variable as a flag inside your callback function.
function myCallBack(varargin)
persistent CallbackRunning
if isempty(CallbackRunning) || ~CallbackRunning
CallbackRunning = true ;
hObject = varargin{1};
mpos = get(hObject,'CurrentPoint');
x = mpos(1);
y = mpos(2);
%perform a complex calculation
varargin{3}.robot.MoveRobotxy(x,y);
CallbackRunning = false ; % do not forget this :)
end

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!