How could I write into the command prompt window of windows 7 with my matlab script?

7 ビュー (過去 30 日間)
Albert
Albert 2015 年 8 月 31 日
コメント済み: Albert 2015 年 9 月 1 日
Hello,
I'm trying to write a line into my command prompt window of Windows 7 with a matlab script, it can open it well but I didn't find how to write in it and simulate 'enter'. Would anyone know how to do that? Thank you in advance!
Lance_filename = 'Script.txt';
Input_filename = 'filename.txt';
fid = fopen(Lance_filename,'w');
script=sprintf('App.exe %s',Input_filename);
fwrite(fid,script,'char');
fclose(fid);
!cmd
%!cmd < Script.txt
%system('cmd.exe &');

採用された回答

Albert
Albert 2015 年 8 月 31 日
ah yeah sure sorry:
Lance_filename = 'Script.txt';
Input_filename = 'filename.txt';
fid = fopen(Lance_filename,'w');
script=sprintf('App.exe %s',Input_filename);
fwrite(fid,script,'char');
fclose(fid);
system(sprintf('%s < "%s"', 'cmd.exe', Lance_filename));
  2 件のコメント
Walter Roberson
Walter Roberson 2015 年 8 月 31 日
I do not understand why you bother creating a file to use as input to cmd.exe when you could just system() the command directly. For example with what you have written you could use
system( sprintf('App.exe "%s"', Input_filename) );
and if it is waiting for any input to proceed you could
system( sprintf('App.exe "%s" < NUL:', Input_filename) );
Albert
Albert 2015 年 9 月 1 日
Ah yes it works fine now! Thank you!

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 8 月 31 日
You could use the Java Robot class, but you should probably instead use the technique I showed here

Albert
Albert 2015 年 8 月 31 日
編集済み: Albert 2015 年 8 月 31 日
Thank you for your answer! The opening works but the written text is "Plusÿ?" instead of the string inside my 'Script.txt' file. How could I correct it?

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by