MATLAB: Stop code execution with a callback

app designerMATLAB

I am integrating cplex in app designer to perfom optimizations. Considering the problem dimensions, sometimes cplex needs a lot of time in order to find a solution. I would like to allow the user to stop the calculation if it seems to be stacked.
I noticed that the command Ctrl+C from the keyboard is able to stop cplex. Cplex is called thanks to a class (then I cannot insert breakpoints inside) and it acts as a function but Ctrl+C does not brake the execution but it asks to cplex to interrupt the optimization finding a partial optimum point.
From app designer the user does not have access to the command window, then is it possible to simulate the command within a callback?
I found this code within the forum. Actually, it works but it depends on the fact that, before starting a function, I must start the timer and then after 4 seconds it executes the command Ctrl+C in the same way I would do from the keyboard.
t = timer('TimerFcn','com.mathworks.mde.cmdwin.CmdWinMLIF.getInstance().processKeyFromC(2,67,''C'')','StartDelay',4);
How can I insert this inside a callbacks and how can I invoke the callback during a ongoing execution? I would like to queue cplex in order to execute Ctrl+C and see that Matlab stops the running.

Best Answer

  • See attached demo. It's for GUIDE but you can use the same concept for App Designer.