How can I run a list of commands using the 'system' function

9 Ansichten (letzte 30 Tage)
Hi all,
I am using the 'system' command to run an external code that requires inputs after the initial system call:
function [stat,cdmout] = meshgen(filename)
cd Run_cases
cd (filename)
[stat,cmdout]=system('./../../../BINARIESLINUX/UOBSMBV2_4_8.x','-echo')
system('g')
system('g')
cd ../..
end
The problem is that the two 'g' calls are not executed and Matlab requires me to input them manually. How can I get Matlab to input them automatically? (The code will run several hundreds of times so getting this right is very useful)
Thanks
  1 Kommentar
Mario Malic
Mario Malic am 9 Nov. 2020
Are you trying to interact with program that is opened by UOBSMBV2_4_8.x?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Nov. 2020
Bearbeitet: Walter Roberson am 9 Nov. 2020
You cannot do that in any easy way. However you can create a file containing the input and do I/O redirection.
tfile = tempname();
fid = fopen(tfile, 'w');
fprintf(fid, 'g\ng\n');
fclose(fid);
cleanMe = onCleanup(@() delete(tfile));
cmd = sprintf('./../../../BINARIESLINUX/UOBSMBV2_4_8.x < "%s"', tfile);
[stat, cmdout] = system(cmd, '-echo');
Note that this approach is not interactive -- you cannot use this to read responses from the program and decide what to send it.
There is also a popen() in the File Exchange, which is unidirectional I/O.
To go beyond that you need to use approaches such as opening a named pipe. Or perhaps there is a Java approach.

Weitere Antworten (0)

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by