Execute Callback Function Multiple Times isnt working in a executable (EXE) but in MATLAB its working
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi,
i have a code which starts a EXE. Then a function checks every 3 seconds if the EXE is still runing (looking into tasklist and a .mat). If EXE is not running and a value is 0 then restart the EXE or close the program. In MATLAB everyhing works fine but when i make a executable it isnt working. It stops after one loop / iteration. Its never checks every 3 seconds if the EXE is running. Enclosed you find both log files (for the MATLAB and for the executable).
%start logging
diary myDiaryFile
fprintf('start Matlab program and logging! %s\n',datestr(now,'HH:MM:SS.FFF'));
%start EXE
system('C:\Users\christian\Desktop\TEST\EXE\Digitale_Ablegeschablone.exe &');
fprintf('start EXE %s\n',datestr(now,'HH:MM:SS.FFF'));
%wait time till EXE is loaded & open
pause(7)
%timer definition
t=timer;
t.period=3;
t.TasksToExecute=inf;
t.ExecutionMode='fixedRate';
t.TimerFcn=@checking;
start(t);
n=1;
%function for checking of EXE is running
function checking(t,~)
fprintf('function is working %s\n',datestr(now,'HH:MM:SS.FFF'));
%load .mat from EXE
load('C:\Users\christian\Desktop\TEST\Parameter.mat');
fprintf('load .mat from EXE %s\n',datestr(now,'HH:MM:SS.FFF'));
%check in tasklist if EXE is running
[~,b]=system('tasklist');
IsRunning=contains(b,'Digitale_Ablegeschablone');
fprintf('check in tasklist if EXE is running %s\n',datestr(now,'HH:MM:SS.FFF'));
%if EXE is not running then check variables value in .mat
if IsRunning(~0)
if Save_Session==1
fprintf('Variable Save_Session is 1 (on). Stop program %s\n',datestr(now,'HH:MM:SS.FFF'));
stop(t)
elseif Block_beendet==1
fprintf('Variable Block_beendet is 1 (on). Stop program %s\n',datestr(now,'HH:MM:SS.FFF'));
stop(t)
else
fprintf('EXE is already open! %s\n',datestr(now,'HH:MM:SS.FFF')');
end
else
%Start EXE again
system('C:\Users\christian\Desktop\TEST\EXE\Digitale_Ablegeschablone.exe &')
fprintf('start EXE again %s\n',datestr(now,'HH:MM:SS.FFF'));
end
end
댓글 수: 3
Rik
2022년 9월 5일
Something is different between your code running in Matlab, and your code running in the compiled exe. One possibility is what the system call returns, as that is the backbone of your code.
I would personally write to an actual log file, whose name I would create with tempname. You chose to use the diary function instead, but you should still be able to see what text is contained in the b char array.
채택된 답변
Walter Roberson
2022년 9월 5일
You create the timer, and then you end the program. Your code "falls off" the end of the script. As far as MATLAB Compiler is concerned, that means that it is time to terminate the executable.
When you execute this code in MATLAB, after the timer is created, your code returns to the MATLAB Command Line and sits at the command line until interrupted by the timer (or the user.) But there is no MATLAB Command Line for .exe to return to.
You need to either code a loop to keep MATLAB alive, or else you need to waitfor() or uiwait() to keep MATLAB running but listening for events.
댓글 수: 8
Rik
2022년 9월 6일
편집: Rik
2022년 9월 6일
It is probably a better idea to call your timer function in the while loop, and use a pause with a small value.
while true
CheckIfRunning
% use drawnow in this function to parse any callbacks and update graphics
[Save_Session,Block_beendet] = LoadUserInteractionFlags;
if Save_Session || Block_beendet
break
end
% Wait a few seconds before rechecking.
pause(3)
end
추가 답변 (1개)
Seth Furman
2022년 9월 12일
I should mention that datestr is discouraged. Prefer datetime where possible.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1122890/image.png)
For example,
dt = datetime("now","Format","HH:mm:ss.SSS")
string(dt)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!