Execute Callback Function Multiple Times isnt working in a executable (EXE) but in MATLAB its working
4 views (last 30 days)
Show older comments
tomy on 5 Sep 2022
Answered: Seth Furman on 12 Sep 2022
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).
fprintf('start Matlab program and logging! %s\n',datestr(now,'HH:MM:SS.FFF'));
fprintf('start EXE %s\n',datestr(now,'HH:MM:SS.FFF'));
%wait time till EXE is loaded & open
%function for checking of EXE is running
fprintf('function is working %s\n',datestr(now,'HH:MM:SS.FFF'));
%load .mat from EXE
fprintf('load .mat from EXE %s\n',datestr(now,'HH:MM:SS.FFF'));
%check in tasklist if EXE is running
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
fprintf('Variable Save_Session is 1 (on). Stop program %s\n',datestr(now,'HH:MM:SS.FFF'));
fprintf('Variable Block_beendet is 1 (on). Stop program %s\n',datestr(now,'HH:MM:SS.FFF'));
fprintf('EXE is already open! %s\n',datestr(now,'HH:MM:SS.FFF')');
%Start EXE again
fprintf('start EXE again %s\n',datestr(now,'HH:MM:SS.FFF'));
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.
Walter Roberson on 5 Sep 2022
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.
Rik on 6 Sep 2022
Edited: Rik on 6 Sep 2022
It is probably a better idea to call your timer function in the while loop, and use a pause with a small value.
% use drawnow in this function to parse any callbacks and update graphics
[Save_Session,Block_beendet] = LoadUserInteractionFlags;
if Save_Session || Block_beendet
% Wait a few seconds before rechecking.
More Answers (1)
Seth Furman on 12 Sep 2022
I should mention that datestr is discouraged. Prefer datetime where possible.
dt = datetime("now","Format","HH:mm:ss.SSS")
Find more on Startup and Shutdown 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!