As Wolfie already mentionned, a standard progressbar is not suited to wait for a process with unknown duration (or unknown iterations). In these cases you better use a spinner (gif file), a circular waitbar (good choice on the File Exchange: cProgress), or an indefinite progressbar. This is what I used for this example:

Now how to make it possible. Since I cannot replicate your exact process foo.cmd, I replaced it by the dos command dir. So for the base line example:
tic
command = 'dir' ;
[~,cmdout] = system(command) ;
toc
>> Elapsed time is 1.547987 seconds.
1.5 second is enough to notice, and cmdout does indeed contain the list of files and directories. So I'll assume this is as close as your case than can be.
To be able to monitor the end of the process, I will package the call to dir (or in your case the call to foo.cmd) into a batch file which will:
- Check if a file (
"MyProcessIsFinished") exist. If yes delete it.
- Call the process of interest, redirect the output to a file.
- Create an empty file
"MyProcessIsFinished"
This allow MATLAB to call the batch file without waiting for the result (using &). You then make MATLAB wait until it detects the file. When detected, you know the process is finished, you can close your waitbar and continue execution of your code. You can then read the file containing the results you used to get in cmdout.
% initialise flag
processFinished = false ;
% start the continuous waitbar
hw = mywaitbar(0.5,'Please wait','Waiting for response ...',true);
% call the process in the batch file, without waiting for result
command = 'mycommand.bat &' ;
system(command) ;
% wait for the process to be finished
while ~processFinished
if exist('MyProcessIsFinished','file')
processFinished = true ;
end
end
close(hw) ; % close the wait bar
% now read your results
cmdout = fileread('outputfile.txt') ;
The file mycommand.bat is now:
@echo off
if exist MyProcessIsFinished del MyProcessIsFinished
dir > outputfile.txt
copy nul MyProcessIsFinished > nul
exit
Don't forget to replace the line dir > outputfile.txt by a call to your process and a redirection to a suitable file name. It could look like:
foo.cmd > ReceivedRequest.json
The continuous waitbar: I picked up mywaitbar.m from the file exchange: mywaitbar. The code is nice but I had to change a couple of things to improve the timer management so if you want a working version there will be a couple of changes:
- Line 109: Add
'CloseRequestFcn',@closeRequestFcn to the properties of the new figure.
- Line 120: Add
'Name','CircularWaitbarTimer' to the properties of the new timer
Then at the bottom of the file, add the following function:
function closeRequestFcn(hobj,~)
% Delete the timer
t = timerfindall('Name','CircularWaitbarTimer') ;
if strcmpi('on',t.Running)
stop(t) ;
end
delete(t)
delete(hobj)
That will make a more stable waitbar utility and will get rid of the annoying warning/error messages about timers not managed properly.
outputor an error mesage if something failed.foo.cmdis a script running on your computer. It could potentially be edited to write a file, as @Hoki suggests, so that your MATLAB process has something to look for.foo.cmd.