How to run an .exe file by one command?

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

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.
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)

Sign in to comment.

Answers (1)

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

The point is that the number I want to "pick" and put it to the .exe file automatic is in one spesific line of an another file. Do you know how to import this value?
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')
ok, after that my .exe file asks me to press "Enter" key from the keyboard. How could I import this to the command:
status = system('file.exe input.txt < number.txt')
??
Have you tried running this line? You will not need to manually press enter using input redirection.
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.
I have tried to put this commands, but this .exe file asks me to press "Enter" after running it. after pressing "Enter" it asks me to put the "input.txt".
So these commands does not work in order to run the .exe program.
Could you help me?
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?
Yes the program:
1)asks me to press enter
2)asks me to put the input file.
So these command
status = system('file.exe input.txt')
does NOT work unfortunately...
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?
running from dos
1)Enter (from keyboard)
2)input.txt
3)value (typing in dos)
4)value (typing in dos)
5)Enter (from keyboard)
See if I understand it correctly?
  1. In dos you type "file.exe" and press "Enter".
  2. Then type "input.txt" and press "Enter".
  3. Type a value like "29.3" and press enter.
  4. Type another value like "5.7" and press enter.
  5. Press "Enter"
Is this correctly describe everything you need to type?
Yes that's right !!
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')
ok, I tried this and it work. My question is if I could pick the number 29.3 from a different file, is there a way to do it?
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.
for K = 1:max(specific_lines)
input_line = fgets(fid) ;
if ismember(K, specific_lines)
input_line{end+1} = input_line;
end
end
Walter, thanks for pointing out. I noticed this now.
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');
Well, to be honest the previous above does not work. Well I have a .txt file (‘data.txt’)
I would like to take the number 11.55 from the file ‘data.txt’ ( it is in the 2nd line and 5th column) and put It to the line 7 of input_2.txt.
How can I do it with the simpliest way?
I am importing the two .txt files in order to understant
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.
ok in order to do ONLY this :
Well, to be honest the previous above does not work. Well I have a .txt file (‘data.txt’)
I would like to take the number 11.55 from the file ‘data.txt’ ( it is in the 2nd line and 5th column) and put It to the line 7 of input_2.txt.
I am importing the two .txt files in order to understant
as I mentioned, which commands could do ONLY this?
Thank you
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?
I know , but I would like to use "less" lines, that why I asked if there is a simpliest way to make it.
But it works , I have tried it!
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.
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()

Sign in to comment.

Categories

Asked:

on 6 May 2020

Commented:

on 15 May 2020

Community Treasure Hunt

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

Start Hunting!