필터 지우기
필터 지우기

Stop code execution with a callback

조회 수: 6 (최근 30일)
Riccardo Barilli
Riccardo Barilli 2020년 7월 28일
답변: Brian Harris 2023년 4월 4일
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);
start(t);
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.

채택된 답변

Image Analyst
Image Analyst 2020년 7월 28일
See attached demo. It's for GUIDE but you can use the same concept for App Designer.
  댓글 수: 1
Riccardo Barilli
Riccardo Barilli 2020년 7월 31일
I can't do that because I cannot insert code inside Cplex.
Actually I resolve the problem this way:
t=timer('TimerFcn',...
strcat(...
'drawnow; ',...
'global curPopupCplex;',...
'disp(''++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++''); ',...
'disp(sprintf(''Stop Cplex? %s'',string(curPopupCplex.StopFlag))); ',...
'if curPopupCplex.StopFlag; ',...
'disp(''Stopping CPLEX ...''); ',...
'com.mathworks.mde.cmdwin.CmdWinMLIF.getInstance().processKeyFromC(2,67,''C'');',...
'end;',...
'disp(''++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++''); '),...
'StartDelay',300,'Period',120,'ExecutionMode','fixedRate');
start(t);
A timer that executes drawnow and resolve the callback queue in order to execute the command that reproduces the Ctrl+C.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Brian Harris
Brian Harris 2023년 4월 4일
If you are ok with the limitations of a parfeval call (need parallel processing toolbox, any graphics produced by the calling function are surpressed, ...) you can easily cancel the call.
% This runs in a parallel worker so does not block the main/calling thread
% note: it will block on a call to fetchOutputs(f)
f = parfeval(backgroundPool,@pause,0,Inf);
% when button is pressed, or other callback is executed, call this:
cancel(f);

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by