Order Matlab not to continue until the external run is completed

Dear all,
I am runing Ansys Fluent in Matlab by the system command and using journal file. I want Matlab to wait until the Fluent run is finished and the results are ready then go to other sections and manipulate data. However, when I run the code it just run fluent and go to the other sections afterward without hesitation. how I can define for Matlab wait until the run is completed then go to the other sections?
BR

4 Comments

Don't tell us; show us...the exact command syntax you are using...and OS
Thank you walter. I used that but it wasnt what I wanted. :(
Dear Walter it is not -gr it was -hidden. Thank you it helped me so much

Sign in to comment.

 Accepted Answer

% delete the 'result.csv'
system('remove result.csv') % add the appropiate path to csv file
% launch your fluent software
system('F:\"Program Files"\"ANSYS Inc"\v195\fluent\ntbin\win64\fluent.exe 2ddp -t4 -i filename.jou'); % RUN ANSYS
% ...
% then do this to wait
while true
if exist('result.csv', 'file') > 0 % add the appropiate path to csv file
break
end
pause(1); % check every second
end
% you might want to wait more until the csv file is fully written by fluent

More Answers (1)

Asuming you are using windows. You can can just add the
[~,~]
as output to the system command, this way matlab will wait until the process is finished. The last of the two outputs would normally gather the command output.
I'm not familiair with the calling fluent from matlab. But in the case of nastran it looks like this:
NastranExec = "C:\Program Files\NXNASTRAN\bin\nastran.exe";
InputFile = "C:\Data\TestFile.bdf";
[~,~] = system([NastranExec,' "',InputFile,'" out="C:\MyRes\" sdir="C:\ScratchDir\" scr=yes old=yes']);

5 Comments

Thank you Friend. For fluent I use the following command for running fluent:
system('F:\"Program Files"\"ANSYS Inc"\v195\fluent\ntbin\win64\fluent.exe 2ddp -t4 -i filename.jou'); % RUN ANSYS
then the fluent must run for maybe 3 or four hours until the results are ready and I have the results to analyze in other sections. does the command [~,~] order the matlab to wait for several hours until fluent runing is finished?
I used that but nothing happened.
hmmm in the case of nastran matlab does wait for the compuation to finish (no matter how long it takes)
Note that the nastran excectuable is the main process that actuall performs the computation, i.e. nastran doesn't call a subprocess. Does fluent call another program for the actual simulation?
I managed the simulation in fluent by journal file. it means that I wrote all steps in a journal file and when I run fluent in matlab it reads the steps in filename.jou and do the simulation. I was thinking about sth else to manage a condition that if the result.csv exists (produced at the end of running) continue the other steps in matlab. This may order the matlab to wait.
When you call system(), whether you use any outputs for the call or not, system() follows these rules:
  • if the command ends in & then the process is run in the background and system() returns as soon as the process starts. This is handled by MATLAB examining the command, regardless of what shell is being used
  • if you are using MacOS or Linux, then some command forms result in the process running in the background. For example system('ls & > ~/MyOutput.txt') would (probably) not have the & detected by MATLAB itself (since the & is not at the end) but Unix-type shells would interpret it as a background request
  • otherwise, MATLAB waits for the command itself to finish. However... if the command causes a graphic program to start, then that graphic program is probably going to be a different process than the command itself, and the command itself would return immediately after starting the graphic program. For example, if you were to start Microsoft Word, then that would launch the graphic user interface and return, and system() would believe that the command is finished (because in a sense it is finished.) It would be a technical challenge to program system() to be able to detect such cases and not return until all the spawned processes were finished.
That last point is why I was talking about the -gr option: if a graphic window is permitted to start, then the graphic window would likely be disconnected from the command that started it, but if you tell the ANSYS not to start graphics then it should operate in "batch mode" and wait for the journal to finish.

Sign in to comment.

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!