How to close a dos window after a .exe is finished running?

조회 수: 8 (최근 30일)
Wesser
Wesser 2022년 9월 1일
댓글: dpb 2022년 9월 4일
When I run the .exe outside matlab i have to press enter to close the dos window. It is only then that I can re-run the .exe for the next simulation. There is a note in the bottom loop where I am hoping to force the dos window to close... how do I script this?
clear all;
clc;
%Solute transport parameters in row 47 of SELECTOR.IN file
SolTrans=[0.035 0 1 0.0479 0 0 0 0 273895 1 0 0 0 0]; %[Kd Nu Beta Henry SnkL1 SnkS1 SnkG1 SnkL1p SnkS1p SnkG1p SnkL10 SnkS10 SnkG10 Alfa] !! UPDATE NUMBERS IN BRACKETS !!
%~~~~~~
%ADD NOISE TO SOLUTE TRANSPORT PARAMETERS
num_sim=100; %100 Monte Carlo Simulations
Kd=SolTrans(1)+.00*rand(1,num_sim); %a vector with 100 values of Kd parameters. We assume that the error is normally distributed with a standard deviation of ##.
Nu=SolTrans(2)+.00*rand(1,num_sim);
Beta=SolTrans(3)+.00*rand(1,num_sim); %!!! CHANGE .02 FOR ALL THESE TO APPROPRIATE STANDARD DEVIATIONSN !!!
Henry=SolTrans(4)+.00*rand(1,num_sim);
SnkL1=SolTrans(5);
SnkS1=SolTrans(6);
SnkG1=SolTrans(7);
SnkL1p=SolTrans(8);
SnkS1p=SolTrans(9)+.00*rand(1,num_sim); %!!! FOR AWI MODIFIED MODEL - KL - LANGMUIR EXPONENT FOR AWI FOR LANGMUIR SORPTION or 0 FOR FREUNDLICH SORPTION
SnkG1p=SolTrans(10);
SnkL10=SolTrans(11);
SnkS10=SolTrans(12);
SnkG10=SolTrans(13);
Alfa=SolTrans(14);
%~~~~~~
%INITIALIZE FILES AND DIRECTORIES
hydrus_exec='C:\\Users\jessi\Desktop\HydrusMC\H1D_CALC.exe';
hydrus_ref= 'C:\\Users\jessi\Desktop\HydrusMC\AFFF1PFOS';
profileDAT= 'C:\\Users\jessi\Desktop\HydrusMC\AFFF1PFOS\PROFILE.DAT';
options= 'C:\\Users\jessi\Desktop\HydrusMC\AFFF1PFOS\Options.in';
atmosph= 'C:\\Users\jessi\Desktop\HydrusMC\AFFF1PFOS\ATMOSPH.IN';
selectorIN= 'C:\\Users\jessi\Desktop\HydrusMC\AFFF1PFOS\SELECTOR.IN';
level= 'C:\\Users\jessi\Desktop\HydrusMC\LEVEL_01.dir';
work_dir= 'C:\\Users\jessi\Desktop\HydrusMC\Simulations\';
mkdir(work_dir);
%~~~~~~
%CREATE DIRECTORIES AND INPUT FILES - This block is doing the work. Creates the input files for all realizations. Copies the mandatory file Profile.dat to
% the corresponding folder and creates the corresponding Selector
% .in. The Selector.in file is different for each realization since the solute transport parameters is
% variable. The code reads the reference Selector.in, copies the first 46 lines to the new file, write the van Genuchten parameters to the 47th line,
% and finally copies the last 10 lines from the reference Selector.in to the new file.
path=cell(1,num_sim);
for i=1:num_sim
% create folder for each run
path{i}=strcat(work_dir,'MC_',num2str(i));
mkdir(path{i});
% Copy input files in MC iteration file
copyfile(profileDAT,path{i});
copyfile(options,path{i});
copyfile(atmosph,path{i});
% Manipulate Selector.in for each run
fileID_out=fopen(strcat(path{i},'\selector.in'),'wt');
fileID_in=fopen(selectorIN);
skip_lines=46; %!!! THIS IS THE LINE THAT CHANGES IN SELECTOR.IN FILE , i.e. solute transport parameters!!!
for k=1:(skip_lines)
x=fgetl(fileID_in); %x = fgetl(fileID)= returns the next line of the specified file, removing the newline characters.
fprintf(fileID_out,'%s\n',x); % %s in the formatSpec input indicates that the values of the variables url and sitename, should be printed as text.
% '\n' as a newline indicator.
% "x" = prints the values from variable x
end
out_Sol=SolTrans; % Renaming...
%Whatever the solute transport parameter is for that monte carlo run (i)
out_Sol(1)=Kd(i); % Solid phase sorption Kd
out_Sol(2)=Nu(i); % Van Genuchten parameter
out_Sol(3)=Beta(i); % Van Genuchten parameter
out_Sol(4)=Henry(i); % Kh = KL*Rmax
out_Sol(9)=SnkS1p(i); % KL - LANGMUIR EXPONENT FOR AWI FOR LANGMUIR SORPTION or 0 FOR FREUNDLICH SORPTION
fprintf(fileID_out,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f\n',out_Sol');
% %f = Fixed-point notation (Use a precision operator to specify the number of digits after the decimal point.)
% '\n' as a newline indicator.
fgetl(fileID_in); % returns the next line of the specified file, removing the newline characters.
skip_lines_end=5; % CORRECT NUMBER??? DOUBLE CHECK
for k=1:(skip_lines_end)
x=fgetl(fileID_in);
fprintf(fileID_out,'%s\n',x);
end
fclose('all');
end
mkdir(work_dir);
for i=1:num_sim
% Rewrite level_01.dir with new file path for each MC iteration
DIR_working = sprintf('%s%d','C:\\Users\jessi\Desktop\HydrusMC\Simulations\MC_',i);
% Print the file path of HYDRUS-1D input files in LEVEL_01.dir
DIR=fopen('C:\\Users\jessi\Desktop\HydrusMC\LEVEL_01.dir','wt+');
fprintf(DIR,'%s\n',DIR_working);
fclose(DIR);
[x, y]= dos(C:\\Users\jessi\Desktop\HydrusMC\H1D_CALC.exe);
if x % unsuccessful
% error('exe failed'); % or take some other action besides throwing an error
end
%THIS IS WHERE I WANT TO CLOSE THE .EXE AFTER IT HAS FINISHED RUNNING. WHEN I RUN THE
%.EXE MANUALLY (OUTSIDE MATLAB, I HAVE TO PRESS ENTER TO CLOSE THE DOS WINDOW...BUT HOW DO
%I DO THIS IN MATLAB?? IN THIS BOTTOM LOOP I WANT TO RUN THE .EXE FOR EACH
%OF THE NUMBER OF SIMULATIONS, BUT THE .EXE WON'T RUN UNLESS THE DOS WINDOW
%IS CLOSED AT THE END OF EACH SIMULATION.
end
Many thanks!

채택된 답변

Fangjun Jiang
Fangjun Jiang 2022년 9월 1일
I have provided this solution in several cases. You just need to have an empty line with a carriage return in that text file.
  댓글 수: 4
dpb
dpb 2022년 9월 1일
That does nothing to stuff a key into the keyboard buffer -- that's a mean trick...
You didn't answer the Q? raised above about the content of the input files -- that's where probably need the extra newline I'm guessing.
Fangjun Jiang
Fangjun Jiang 2022년 9월 2일
The correct way is:
CmdStr=[hydrus_exec, ' < ', CloseEXE];
system(CmdStr)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

dpb
dpb 2022년 9월 1일
이동: Image Analyst 2022년 9월 1일
Use a .bat file instead -- that runs the desired executable and then ends with the 'exit' command.
  댓글 수: 4
hydrogall
hydrogall 2022년 9월 1일
honestly, I'm confused by your comment. The exe runs fine and the bottom loop shown below in the screen shot is the only section that I'm having trouble with. the rest of the code is copacetic. the gist of this entire code tho is that I am making a bunch of subdirectories, each with different input files and running the .exe with each subdirectory of input files...ie running a monte carlo The input files are not the problem, of that i am certain.
the only section that is the issue is that once the .exe is sucessfully run with the 1st set of input files, then the .exe doesn't close out/end/terminate/however you want to say it...without manually pressing the enter key on my keyboard. once i do press the enter key the .exe will run again with the 2nd set of input files...but once that iteration is finished i need to push the enter button again...and so forth. how do i get matlab to magically "press" the enter button for me so that the loop runs through all the monte carlo iterations without me having to press enter over and over??
dpb
dpb 2022년 9월 1일
You said it acts the same way from the command that you have to press a return to close the window...that shouldn't be; the window should be back at the command prompt. If it isn't, that implies to me the app is still expecting input.
If the command window is actually at the command prompt to type in "exit" to close it, then the batch file option will work -- the .bat/.cmd file content would simply be
yourexecutable command line first
exit
then you submit it instead of the command to just run the executable.

댓글을 달려면 로그인하십시오.


dpb
dpb 2022년 9월 1일
편집: dpb 2022년 9월 2일
Actually, the line
[x, y]= dos(C:\\Users\jessi\Desktop\HydrusMC\H1D_CALC.exe);
can't work -- the argument to dos must be a character string and the above isn't...
Try
[status,res]=dos('C:\Users\jessi\Desktop\HydrusMC\H1D_CALC.exe');
instead -- and the executable should run in background without any command window visible at all.
I just tried a little Fortran test routine here as well as a form of your mistyped line above to see if the system command would do something funky like try to open a system prompt window but, as expected it errs on the bad syntax before getting that far, so this cannot be the exact code you're running to have a problem with.
>> [s,r]=dos(C:\\GCC\bin\tmp\testit.exe)
[s,r]=dos(C:\\GCC\bin\tmp\testit.exe)
Error: Invalid use of operator.
>>
That fails as would expect it to...
Let's run a little Fortran code -- it doesn't need any input files, but does execute w/o a visible window ever opening as should...
>> [s,r]=dos('C:\GCC\bin\tmp\testit.exe')
s =
0
r =
'zeros(6,4)
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
eye(4)
1.000 0.000 0.000 0.000
0.000 1.000 0.000 0.000
0.000 0.000 1.000 0.000
0.000 0.000 0.000 1.000
'
>>
(I was checking out if new allocatable arrays were working in gfortran...)
I then modified the code to ask user for an input value -- that hangs the process awaiting that last input, but still doesn't open a visible window -- somewhat to my surprise, the default input stream is still attached through the MATLAB command window -- typing into it puts the response into the buffer for the app and lets it close successfully.
Think would have to see just how your executable runs and how it gets its input to have further insights on your issues.
Which OS we running under here?
  댓글 수: 7
Wesser
Wesser 2022년 9월 4일
This ended up working!! Many thanks for your time on this!
dpb
dpb 2022년 9월 4일
Yeah, once figured out that it wasn't the command shell were needing to exit from but the application itself asking for user input, the whole problem changed drastically. @Fangjun Jiang presumed that from the git-go, I read the original post literally instead... :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by