When starting the external program (let's call it "EX" for brevity) from within Matlab I can either do it like this
[status, result] = system('EX.exe');
which blocks until EX returns, or like this
[status, result] = system('start EX.exe');
which returns to Matlab immediately but can't fetch EX's return code.
It would be nice to still have EX's return code available in Matlab once it is done. This would be the easiest way for the calling Matlab script to notice any problems EX might run into. On the other hand, I would like Matlab to do other stuff while EX is running, e.g. displaying information on progress. So, the call needs to be non-blocking.
I tried to work around this obvious conflict by starting EX as described in the first example above. To be able to run some other code (displaying progress information) while EX is busy, I put this code into a function and called that by using a timer with a small StartDelay.
Unfortunately this does not provide real multithreading (something that Matlab seems to be incapable of without Parallel Computing Toolbox), i.e. if the code in the timer callback runs longer than EX, execution again blocks until the timer callback returns. Even worse: Since I don't know how long EX will run, the timer callback must run endlessly until it is either stopped or passed some flag that tells it to stop. I tried with global variables or even storing this flag in application data like this:
setappdata(0, 'running', 1);
tim = timer(...
'StartDelay', 2, ...
'TimerFcn', 'while getappdata(0, ''running''), fprintf(''timer running ...\n''); pause(1); end' ...
);
fprintf('Starting timer.\n');
start(tim);
fprintf('Calling external program ...\n');
[s,r] = system('EX.exe');
fprintf('External program returned %d.\n', s); % <-- This is never reached.
setappdata(0, 'running', 0);
fprintf('Stopping timer.\n');
stop(tim);
delete(tim);
The code following the call to system() never seems to be executed and the timer callback runs forever ... Is there any other way I can get this to work or is it really either system's return value OR non-blocking call?