1

I am trying to get continued interaction with a command line window once I give an initial command. The command is:
system('start cygwinbatch.bat')

which contains:

::@echo off
C:\cygwin64\bin\bash -li /cygdrive/c/stp/stpscript.sh

This is to run the "stpscript.sh" which contains only one line, which runs stp.exe in the cygwin environment (which it requires). This all works properly, however stp has an in-command-line interface that requires additional input (it allows me to download seismic data to the current folder).

What I want to know is, can I make additional commands to that already opened window through MATLAB? Additional commands (with new lines) in MATLAB seem to just create new command line instances. STP takes about two seconds to become operational and ready for new commands once I open it, so if MATLAB will try to give it a flurry of inputs (as in open STP, then immediately try to download data) it may not work properly.

I was also thinking I could maybe add more lines to my .sh script with additional commands, containing what to download, but the information as to which data I need to access is all calculated within my MATLAB scripts. It would be a lot easier if I didn't have to continually rewrite my .sh file with the next dataset to download, and I could just give MATLAB a new command instead. So, can this be done?

Stack Player
  • 1,470
  • 2
  • 18
  • 32
Aaron A.
  • 33
  • 1
  • 4
  • 1
    Not a very elegant solution, but you can [programmatically send key presses](http://stackoverflow.com/questions/27933270/programmatically-press-an-enter-key-after-starting-exe-file-in-matlab) from Matlab using Java (here's a [list of key codes](http://docs.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html)) or .NET. – Luis Mendo Aug 21 '15 at 10:36
  • Hey, thanks a lot for this advice. I used it and it worked properly. Those other questions were pretty helpful. I wish I could give you the best answer for this cause I'm pretty sure it's the only thing that would work anyways. – Aaron A. Aug 26 '15 at 22:36
  • I'm glad you found it useful! – Luis Mendo Aug 27 '15 at 02:00

1 Answers1

-1

I don't know of a way to continuously interact with an external program, however if you know the input to stp.exe beforehand, you could use I/O redirection.

For example, you could generate a file containing the input parameters:

A = 1;
B = 2;
f = fopen('C:\my\path\input.txt','w+')
fprintf(f,'A = %d\nB=%d\n',A,B);
fclose(f)

Then you redirect the standard input to said file in stpscript.sh

stp.exe < C:\my\path\input.txt

and read your program's output:

[~,output] = system('start cygwinbatch.bat')

You could of course also generate shell scripts from MATLAB instead of manually rewriting them.

dasdingonesin
  • 1,347
  • 1
  • 10
  • 16