Communicating with an external program

10 次查看(过去 30 天)
I am using a program called stp found here: http://scedc.caltech.edu/research-tools/downloads.html
Essentially what I am trying to do is within my matlab script have it use stp and request downloads of data that I have outlined in the script. The issue is from what I have found so far, system() doesn't allow me to continually communicate with the program (as in, send it additional commands).
stp cannot be called with any additional options or already telling it what file you want it to send you, you have to wait for it to connect first, and then send a request. My biggest problem is that once I send the command to run stp matlab sits and waits for it, but it's really waiting for another input itself. So when I run it I will get:
>> stprun %example name of script that at one point has a system('stp') command
STP: connected to caltech... etc, a lot of other stuff. basically what I would see if I ran this from any terminal.
STP>
And that's basically where it stops and waits. I don't know how to give additional inputs into "STP>", which is waiting for inputs, because matlab is also waiting for a return. I have to manually type "exit" and then hit enter which will give matlab nothing back and then the script will fail. I don't really care about a return, I just want to be able to tell STP what to do, and then have the script continue. Like send keypresses to the terminal somehow, once I start STP.
If it matters any, I'm running this on Linux.

采纳的回答

Walter Roberson
Walter Roberson 2016-9-24
There is a popen() in the File Exchange that can be used in Linux and OS-X . It does not support bidirectional transfers, but if you do not need to analyze the responses then you should be okay by just redirecting the output to a file (and possibly reading the file afterwards.)
On the other hand, if it is just a matter of sending input to a command line program without needing to interact with it, then there is a much easier way: write all of the desired commands into a file, and then when you system() redirect standard input from the file.
  2 个评论
Aaron Anderson
Aaron Anderson 2016-9-26
Hi, thanks for your reply. The second solution you gave seems to be the most useful for what I am doing, as I don't really need to know what the program is telling me. But can you give an example of how you redirect input? I tried searching documentation and didn't find anything.
Walter Roberson
Walter Roberson 2016-9-26
tfile = tempname();
fid = fopen(tfile, 'wt');
fprintf(fid, 'First line of input\nsecond line of input\n')
fprintf(fid, 'Input line %d\n', 3:6);
fclose(fid);
scmd = sprintf('stprun < ''%s''', tfile);
[status, cmdoutput] = system(scmd);
delete(tfile);

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2016-9-24
Perhaps java robot???
% Demo to use java robot to push buttons on a web page
clc;
import java.awt.Robot
import java.awt.event.*
mouse = Robot;
% Press a push button on the page.
% Move to pixel (330, 455) on the computer screen.
mouse.mouseMove(330, 455); % First message.
mouse.mousePress(InputEvent.BUTTON1_MASK);
mouse.mouseRelease(InputEvent.BUTTON1_MASK);
pause(1); % Give time for page to refresh.
% To perform right click (BUTTON3) and get the menu(works fine)
mouse.mouseMove(270, 505);
mouse.mousePress(InputEvent.BUTTON1_MASK);
mouse.mouseRelease(InputEvent.BUTTON1_MASK);
% Click next page button.
pause(2); % Give time for page to refresh.
mouse.mouseMove(940, 422); % Next page.
mouse.mousePress(InputEvent.BUTTON1_MASK);
mouse.mouseRelease(InputEvent.BUTTON1_MASK);
% Put mouse over MATLAB run button.
mouse.mouseMove(1520, 74);
fprintf('Done.\n');

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by