Execute Callback Function Multiple Times isnt working in a executable (EXE) but in MATLAB its working

4 visualizzazioni (ultimi 30 giorni)
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 Commenti
Rik
Rik il 5 Set 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.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 5 Set 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.
  8 Commenti
tomy
tomy il 6 Set 2022
Modificato: tomy il 6 Set 2022
is this OK or is there a better way with better performance and low CPU-Load? So i dont really need a function? I can all do within the while true? Thank You
while true
if Save_Session==1 | Block_beendet==1
break
end
pause(inf)
end
Rik
Rik il 6 Set 2022
Modificato: Rik il 6 Set 2022
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

Accedi per commentare.

Più risposte (1)

Seth Furman
Seth Furman il 12 Set 2022
I should mention that datestr is discouraged. Prefer datetime where possible.
For example,
dt = datetime("now","Format","HH:mm:ss.SSS")
dt = datetime
15:45:11.683
string(dt)
ans = "15:45:11.683"

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by