Execute Callback Function Multiple Times isnt working in a executable (EXE) but in MATLAB its working
Show older comments
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 Comments
Rik
on 5 Sep 2022
Did you check the output of the system call when running in your compiled exe?
tomy
on 5 Sep 2022
Rik
on 5 Sep 2022
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.
Accepted Answer
More Answers (1)
I should mention that datestr is discouraged. Prefer datetime where possible.

For example,
dt = datetime("now","Format","HH:mm:ss.SSS")
string(dt)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!