How to run an .exe file by one command?
Show older comments
Hello,
I have a question about a code. I would like to ask a question. I would like to run an .exe file via matlab. My .exe first of all requires an input file in order to run. For this purpose I use the command:
status = system('file.exe input.txt')
Secondly, after putting this command, the .exe file requires to type a specific number in order to run. I would like to avoid typing by hand this number and taking it from an another .txt file .
Could anyone help me?
2 Comments
Rik
on 6 May 2020
If you don't want to type it manually, you need to make sure you can enter it through the command line itself. Note that using a batch file to do this is also an option, if you can't get everything on a single line of code.
Mario Malic
on 6 May 2020
Could you refer to the .exe program you are calling if there's an option to add it as a parameter?
Example:
status = system('file.exe input.txt' -p 1)
Answers (1)
Ameer Hamza
on 6 May 2020
You can use input redirection to type the input from a txt file. Something like this
status = system('file.exe input.txt < number.txt')
25 Comments
Ivan Mich
on 14 May 2020
Walter Roberson
on 14 May 2020
fid = fopen('AnotherFile.txt', 'rt');
for K = 1 : spesific_line; input_line = fgets(fid); end
fclose(fid)
fid = fopen('number.txt', 'wt');
fwrite(fid, input_line);
fclose(fid);
status = system('file.exe input.txt < number.txt')
Ivan Mich
on 14 May 2020
Ameer Hamza
on 14 May 2020
Have you tried running this line? You will not need to manually press enter using input redirection.
Walter Roberson
on 14 May 2020
You could try
fid = fopen('number.txt', 'wt');
fwrite(fid, input_line);
fprintf(fid, '\n');
fclose(fid);
but sometimes programs hook into keypress inputs and then it gets messy... messy as in Java Robot Class.
Ivan Mich
on 14 May 2020
Ameer Hamza
on 14 May 2020
You said that it asks you to put "input.txt". Does this mean that even the line without redirection does not work?
status = system('file.exe input.txt')
Does this line work?
Ivan Mich
on 14 May 2020
Ameer Hamza
on 14 May 2020
I got a bit confused. It appeared from your question that this command already works. It might be the case that your file.exe file is not created to take input argument from the command line. Can you tell what the commands you use to manually run file.exe from the command line (not from MATLAB) are? How does the file.exe normally works outside of MATLAB?
Ivan Mich
on 14 May 2020
Ameer Hamza
on 14 May 2020
See if I understand it correctly?
- In dos you type "file.exe" and press "Enter".
- Then type "input.txt" and press "Enter".
- Type a value like "29.3" and press enter.
- Type another value like "5.7" and press enter.
- Press "Enter"
Is this correctly describe everything you need to type?
Ivan Mich
on 14 May 2020
Ameer Hamza
on 14 May 2020
Before writing any code. I suggest you try running the following lines in MATLAB and see does it works.
Create a file named number.txt and write the following lines in it
input.txt
29.3
5.7
also make sure input.txt is also in the same folder and the run following command in MATLAB
status = system('file.exe < number.txt')
Ivan Mich
on 14 May 2020
Ameer Hamza
on 14 May 2020
Edited: Ameer Hamza
on 14 May 2020
Walter's answer already shows you the way to read data from another file and then pass it to file.exe. The actual issue was that the syntax you described in the question does not work. Now that issue is sorted out. You can try this modified version of Walter's code
specific_lines = [15 18]; % line numbers you want to read
count = 1;
input_line = {};
fid = fopen('AnotherFile.txt', 'rt');
for K = 1:specific_lines
this_line = fgets(fid);
if ismember(K, specific_lines)
input_line{end+1} = this_line;
end
end
fclose(fid)
fid = fopen('number.txt', 'wt');
fwrite(fid, sprintf('input.txt\n'));
fwrite(fid, sprintf('%s\n', input_line{1}));
fwrite(fid, sprintf('%s\n', input_line{2}));
status = system('file.exe < number.txt')
Edited according to Walter's next comment.
If there is still some issue, attach the sample data file from which you want to load the numbers.
Walter Roberson
on 14 May 2020
Edited: Walter Roberson
on 14 May 2020
for K = 1:max(specific_lines)
input_line = fgets(fid) ;
if ismember(K, specific_lines)
input_line{end+1} = input_line;
end
end
Ameer Hamza
on 14 May 2020
Walter, thanks for pointing out. I noticed this now.
Walter Roberson
on 14 May 2020
I recommend changing the writing method:
fid = fopen('number.txt', 'wt');
fprintf(fid, 'input.txt\n');
fwrite(fid, strjoin(input_line, '');
fclose(fid);
status = system('file.exe < number.txt');
You would not use %s\n for the output strings because you used fgets so the newline is part of what you recorded.
But it would also be reasonable to use:
specific_lines = [15 18]; % line numbers you want to read
count = 1;
input_line = {};
fid = fopen('AnotherFile.txt', 'rt');
for K = 1:max(specific_lines)
this_line = fgetl(fid);
if ismember(K, specific_lines)
input_line{end+1} = this_line;
end
end
fclose(fid)
fid = fopen('number.txt', 'wt');
fprintf(fid, 'input.txt\n');
fprintf(fid, '%s\n', input_line{:});
fclose(fid);
status = system('file.exe < number.txt');
Ivan Mich
on 15 May 2020
Walter Roberson
on 15 May 2020
datafile = 'data.txt'; %read from it
inputfile = 'input_2.txt'; %read from it
newinputfile = 'input.txt'; %write to it
fidin = fopen(datafile, 'rt');
if fidin < 0; error('failed to open "%s" for reading', datafile); end
fgetl(fidin);
number5 = fscanf(fidin, '%*s%*s%*s%*s%f', 1);
fclose(fidin);
fidin = fopen(inputfile, 'rt');
if fidin < 0; error('failed to open "%s" for reading', datafile); end
fidout = fopen(newinputfie, 'wt');
if fidout < 0; fclose(fidin); error('failed to open "%s" for writing', newinputfile); end
for K = 1 : 6
input_line = fgetl(fidin);
fprintf(fidout, '%s\n', input_line);
end
input_line = fgetl(fidin);
fprintf(fidout, '%g\n', number5);
while ischar(input_line)
input_line = fgetl(fidin);
if ~ischar(input_line); break; end %end of file
fprintf(fidout, '%s\n', input_line);
end
fclose(fidin);
fclose(fidout);
cmd = sprintf('file.exe < "%s"', newinputfile);
status = system(cmd);
If you were wanting to do this a number of times with different data files but the same input_2.txt file (being modified each time) then I would probably use a different approach.
Ivan Mich
on 15 May 2020
Ameer Hamza
on 15 May 2020
Ivan, Walter code does exactly that. It reads 11.55 from 'data.txt' and writes it to the 7th line of 'input_2.txt' while leaving other lines as it is. It writes the output to a file named 'input.txt'. Have you tried running the code?
Ivan Mich
on 15 May 2020
Ameer Hamza
on 15 May 2020
You can get away with few lines in Walter's code if you know that there will be no issue with reading and writing to a file.
if fidin < 0; error('failed to open "%s" for reading', datafile); end
and other such lines. Apart from that, any further attempt to make code compact will usually make the logic more complicated.
Walter Roberson
on 15 May 2020
The above is the simplest way to do what you ask: it uses only basic I/O operations. But simplest is not the same as shortest.
datafile = 'data.txt'; %read from it
inputfile = 'input_2.txt'; %read from it
newinputfile = 'input.txt'; %write to it
lines = regexp(fileread(inputfile), '\r?\n','split');
number5 = textscan(fileread(datafile),'%*s%*s%*s%*s%s', 1, 'headerlines', 1);
lines(7) = number5{1};
fid = fopen(newinputfile,'wt'); fprintf(fid, '%s\n', lines{:}); fclose(fid);
status = sytem( sprintf('file.exe < "%s"', newinputfile) );
To understand this you have to understand something about the text processing utility regexp(), and several of the less-common details about textscan(), and understand how cell expansion interacts with fprintf()
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!