Can't get matlab to run a .exe file...

11 views (last 30 days)
Wesser
Wesser on 13 Jun 2022
Commented: Voss on 14 Jun 2022
Here is my code:
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 some of solute TRANSPORT PARAMETERS
num_sim=100; %100 Monte Carlo Simulations
Kd=SolTrans(1)+.02*rand(1,num_sim);
Nu=SolTrans(2)+.02*rand(1,num_sim);
Beta=SolTrans(3)+.02*rand(1,num_sim);
Henry=SolTrans(4)+.02*rand(1,num_sim);
SnkL1=SolTrans(5);
SnkS1=SolTrans(6);
SnkG1=SolTrans(7);
SnkL1p=SolTrans(8);
SnkS1p=SolTrans(9)+.02*rand(1,num_sim);
SnkG1p=SolTrans(10);
SnkL10=SolTrans(11);
SnkS10=SolTrans(12);
SnkG10=SolTrans(13);
Alfa=SolTrans(14);
%~~~~~~
%INITIALIZE FILES AND DIRECTORIES
hydrus_exec='C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe';
hydrus_ref='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS';
profileDAT='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\PROFILE.DAT';
selectorIN='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\SELECTOR.IN';
work_dir='C:\Users\jessi\Desktop\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 are
% variable. The code reads the reference Selector.in, copies the first 45 lines to the new file,
% writes the van Genuchten parameters to the 46th line,
% and finally copies the last 5 lines from the reference Selector.in to the new file.
path=cell(1,num_sim);
for i=1:num_sim
path{i}=strcat(work_dir,'run_',num2str(i)); %create folder for each run
mkdir(path{i});
copyfile(profileDAT,path{i}); %copy profileDAt from reference directory to the simulation directory
fileID_out=fopen(strcat(path{i},'\selector.in'),'wt'); %manipulate Selector.in for each run
% "wt"= permision for file axis type
% w=Open or create new file for writing. Discard existing contents, if any.
% To open files in text mode, attach the letter 't' to the permission argument,
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;
for k=1:(skip_lines_end)
x=fgetl(fileID_in);
fprintf(fileID_out,'%s\n',x);
end
fclose('all');
end
%~~~~~~
%THIS NEXT SMALL SECITON IS WHERE I THINK I'M GETTING INTO TROUBLE....?
for i=1:num_sim
exec_path=[hydrus_exec ' ' path{i}];
[x, y]= dos(exec_path);
end
%~~~~~~
%READ HYDRUS OUTPUT FILES - This block reads for all the realizations of
%the Nod_inf.out file and stores the water content profiles for t=10 days in Prof_out table.
prof_out=zeros(101,num_sim+1);
for i=1:num_sim
fileID_out=fopen(strcat(path{i},'\Obs_Node.out'));
skip_lines=11;
for k=1:(skip_lines)
x=fgetl(fileID_out); %THIS IS THE LINE WHERE THE ERROR IS HAPPENING....
end
temp=fscanf(fileID_out,'%f',[5,55951])'; %temporarily scan matrix of 5 columns by 55951 rows (days in hydus)
prof_out(:,i+1)=temp(:,5); %save the 5th column of each realization (the concentration at the node)
fclose(fileID_out);
end
prof_out(:,1)=temp(:,2);
I'm basically creating a buch of path{i}, trying to get the hydrus.exe to run for each path{i} and they trying to save the output from each realization of the .exe model run. The error I'm getting cites "x=fgetl(fileID_out); " down at the bottom of the code...but I think fgets1 is having trouble calling the Obs_Node.out file that should be generated each time hydrus_exec is being called....Basicaly I don't think I am getting matlab to run hydrus.exe (aka hydrus_exec) for each of the directories I created....but I don't know why. Each directory has a seperate set of different input files needed to run hydrus.exe.
Any help/suggestions are VERY welcome!! Thanks!

Answers (1)

Voss
Voss on 13 Jun 2022
Edited: Voss on 13 Jun 2022
Try putting double-quotes around the paths contained in exec_path (i.e., the path to the exe and the directory sent to the exe as an argument):
hydrus_exec = 'C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe';
work_dir='C:\Users\jessi\Desktop\Simulations\';
i = 1;
path{i} = strcat(work_dir,'run_',num2str(i));
exec_path_old = [hydrus_exec ' ' path{i}]
exec_path_old = 'C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe C:\Users\jessi\Desktop\Simulations\run_1'
exec_path_new = ['"' hydrus_exec '" "' path{i} '"']
exec_path_new = '"C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe" "C:\Users\jessi\Desktop\Simulations\run_1"'
(The double-quotes are necessary when there is a space in the path name, but it doesn't hurt to have them there regardless.)
Alternatively, you could include the double-quotes in hydrus_exec and path, e.g.:
hydrus_exec = '"C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe"';
path{i} = strcat('"',work_dir,'run_',num2str(i),'"');
exec_path = [hydrus_exec ' ' path{i}]
exec_path = '"C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe" "C:\Users\jessi\Desktop\Simulations\run_1"'
but that requires making sure your code can handle hydrus_exec and path with double-quotes, whereas the first way just puts them there when the exe is run.
Also, you might take advantage of the status returned from dos, so you know right away whether the exe was run successfully:
[x, y]= dos(exec_path); % x == 0 -> success
if x % unsuccessful
error('exe failed'); % or take some other action besides throwing an error
end
  3 Comments
Steven Lord
Steven Lord on 14 Jun 2022
Show us the value of the second output from your dos call when x is non-zero. That may contain information that indicates the specific problem that caused MATLAB to be unable to call that executable and/or that suggests how to solve the problem.
Voss
Voss on 14 Jun 2022
Try it with the double-quotes only in one place. As it is now you've got double-double-quotes because you've got them here:
hydrus_exec='"C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe"';
and here:
exec_path=['"' hydrus_exec '" "' path{i} '"'];
Just do one or the other, not both.
For instance, this way:
hydrus_exec='C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe'; % no double-quotes
% ...
exec_path=['"' hydrus_exec '" "' path{i} '"']; % double-quotes get put in here

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!