nhathahn has asked for the wisdom of the Perl Monks concerning the following question:

Dear friends,

I have seen many answers to questions concerning running other programs from a Perl script, however, I have not seen a question similar to mine.

I wish to manipulate the input file for another program (for example, a Matlab .m file). I then want to open the program, run the input file, and read the output file.

The part I am having trouble with involves the execution of the input file. I can manipulate the input file and open the program, but I cannot execute the input file. My reading of exec() and system() documentation does not lead me to believe these are the appropriate commands.

Please advise me. And thank you for your efforts.

Replies are listed 'Best First'.
Re: Interacting with other programs
by Sixtease (Friar) on Nov 23, 2007 at 17:42 UTC

    If I understand your question well, then you're normally executing a matlab file by typing ./myscript.m. That will launch matlab and feed it with the script, if there's something like #!/usr/bin/matlab. Or you are used to double-click such files in a GUI-enabled file manager and the operating system has the files associated with the appropriate interpreter (like matlab in your case) and runs it for you.

    You simply need to explicitly say that you want to run matlab and pass it your script as an argument. That is, instead of saying "myscript.m", say "matlab myscript.m", since that's what happening behind the scenes anyway.

    A note: I have never even seen Matlab, so the things about launching Matlab scripts and the #!/usr/bin/matlab line are pure assumptions.

    Update: It just occured to me that you might be "running the input file" in interactive mode. If you run Matlab, it gives you a command line and from there you type a command to process a file, and you want to do this from perl, then things will be a bit more complicated, I fear.

    I'd suggest trying to find a way to tell Matlab to process the file from command line (there should be an option or maybe the syntax I used in the above example could work). If that can't be done, then I guess you'll have to feed the command to the interpreter via STDIN. That would mean, if you normally write:

    OS prompt $ matlab ... Matlab welcome message ... Matlab prompt $ run my_script.m

    then you'd say in perl: system ('echo "run my_script.m" | matlab')

Re: Interacting with other programs
by Joost (Canon) on Nov 23, 2007 at 17:26 UTC
Re: Interacting with other programs
by zentara (Cardinal) on Nov 23, 2007 at 19:33 UTC
    You might want to try IPC::Open3. You should be able to open the matlab executable thru the pipe, then print to it to load .m files. This is untested of course, and many executable won't work that way, but it's worth a try. Does matlab allow you to issue commands to it after it's running? Like "load myscript.m". If it does, you have a chance with running it thru a pipe with IPC. Read "perldoc perlipc".

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Interacting with other programs
by planetscape (Chancellor) on Nov 24, 2007 at 06:50 UTC

    If you are on a Windows system (you don't say), "interacting with other programs" (usually called "automation") often involves modules such as Win32::OLE. In your specific case, I think you would benefit greatly by looking up Math::Matlab and seeing if one or more of the search results does what you need.

    HTH,

    planetscape