Hi,
I am running an external .exe file that returns something to the matlab command line once it has finished. I would like to use this in an optimization but currently the enter key needs to be pressed in the command line in order for the .exe to close and then for the matlab code to continue.
Here's how i'm calling the .exe: eval(['!program',tempfilename]);
Is there a way I can continue after it has finished? I have tried a timer and the java robot, but neither seem to work.
Thanks,

1 件のコメント

Rik
Rik 2021 年 6 月 9 日
You will have to find a way to get your program to work without interaction.
Also, why aren't you using system instead of eval?

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

 採用された回答

Joel Lynch
Joel Lynch 2021 年 6 月 9 日
編集済み: Joel Lynch 2021 年 6 月 9 日

0 投票

You can use system() function to pass the command (as a string) without the "!" symbol. If using unix OS, you can pipe (with the | character) a newline character entry ( with echo -e '\n') to a program. System will wait until both commands are completed. Note: the double apostrophes around \n are needed as escape characters and print an apostrophe.
system(['echo -e ''\n'' | ',some_exe_str]);
Not sure if using Windows...

8 件のコメント

SamiamO
SamiamO 2021 年 6 月 9 日
Sorry, yes using Windows. Thanks, i'll try this!
Joel Lynch
Joel Lynch 2021 年 6 月 9 日
echo and piping should work in system() on windows, but the key entry may not. I would look around other forums for ways to simualte keystrokes in windows command line. Whatever approach you find just needs to replace the "echo -e '\n'" bit, and you can test the command in powershell outside of matlab first.
SamiamO
SamiamO 2021 年 6 月 9 日
編集済み: Walter Roberson 2021 年 6 月 10 日
status = system(sprintf('Program "%s" <NUL',tempfilename));
This seems to work using <NUL but then I get the following on the command line which might be a problem:
forrtl: severe (24): end-of-file during read, unit -4, file CONIN$
Image PC Routine Line Source
Program.exe 008AC610 Unknown Unknown Unknown
Joel Lynch
Joel Lynch 2021 年 6 月 9 日
That looks like an internal error of the executable you are running. Are you sure the executable would run correctly outside of MATLAB?
SamiamO
SamiamO 2021 年 6 月 9 日
Hi,
This has worked for me: status = system(sprintf('echo. | program "%s" ',tempfilename));
Thanks so much for your help!
Joel Lynch
Joel Lynch 2021 年 6 月 9 日
Glad to hear it. What does the period after echo do?
You shouldn't need to use sprintf though, this should work too and be simpler:
status = system( [ 'echo. | program ',tempfilename] );
assuming tempfilename is a string.
Rik
Rik 2021 年 6 月 9 日
Or with the double quote:
status = system( [ 'echo. | program "' tempfilename '"'] );
SamiamO
SamiamO 2021 年 6 月 10 日
Thanks all, working perfectly now! - also the period is not necessary, thanks for pointing that out.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2020a

タグ

質問済み:

2021 年 6 月 9 日

編集済み:

2021 年 6 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by