1

MATLAB UI callbacks (e.g. WindowKeyPressFcn) can be interrupted by newly triggered callbacks. This is very useful for terminating code which is taking too long to run. Is there any way to programmatically trigger a callback event?

Note that, in contrast to the answers to this question, I am not asking how to run a callback function programmatically. I actually want to trigger the callback event programmatically, to make use of this interruptibility feature.

Community
  • 1
  • 1
user664303
  • 2,053
  • 3
  • 13
  • 30

1 Answers1

2

Callback interruption is initiated by other callbacks specifically because there can't be any other code executing while the callback is executing. If you're in a position to "programmatically" invoke a callback, you already have "interrupted" any other callback that might be in progress.

That isn't to say that you can't trigger UI callbacks programmatically. Callback handles stored in object properties like WindowKeyPressFcn generally feed into listeners for corresponding events for those objects. For example you can invoke WindowKeyPressFcn for the current figure using notify(gcf,'WindowKeyPress').

However if you're in a position to make that call, you can also do anything you'd like to in the interrupting callback. If you're able to run that line because you are interrupting a callback, you then need to consider what the callback will do when it resumes execution.

Will
  • 1,835
  • 10
  • 21
  • You've made a good point. However, I wonder if timer functions operate in a separate thread, so can be used to call `notify(gcf(), 'WindowKeyPress')` while the WindowKeyPressFcn is currently running. – user664303 Dec 22 '15 at 15:35
  • No, my suggestion doesn't work. Strange, because timers can interrupt standard matlab code. Also, good point about what to do when it resumes anyway. – user664303 Dec 22 '15 at 15:58