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

3 visualizaciones (últimos 30 días)
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 comentario
Mario Malic
Mario Malic el 9 de Nov. de 2020
Are you trying to interact with program that is opened by UOBSMBV2_4_8.x?

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Nov. de 2020
Editada: Walter Roberson el 9 de Nov. de 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.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by