How to run an .exe file by one command?

10 ビュー (過去 30 日間)
Ivan Mich
Ivan Mich 2020 年 5 月 6 日
コメント済み: Walter Roberson 2020 年 5 月 15 日
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 件のコメント
Rik
Rik 2020 年 5 月 6 日
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
Mario Malic 2020 年 5 月 6 日
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)

サインインしてコメントする。

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 6 日
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 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 5 月 15 日
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
Walter Roberson 2020 年 5 月 15 日
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()

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by