Execute Callback Function Multiple Times isnt working in a executable (EXE) but in MATLAB its working
6 次查看(过去 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.
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!