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

OK, a slighty different twist on my last MATLAB question - how can I call MATLAB from Perl in order to create a .mat file? ..thanks in advance!
  • Comment on calling MATLAB from perl in order to create a .mat file

Replies are listed 'Best First'.
Re: calling MATLAB from perl in order to create a .mat file
by kennethk (Abbot) on Jan 19, 2010 at 16:17 UTC
    It's been a while since I've used MATLAB from the command line, but the basic approach you should be taking is invoking the OS to run the MATLAB script in question, and telling that script to generate your desired .mat file.

    In order to invoke the operating system with Perl, there are two basic approaches you can take.

    1. If you want to capture output from the process, then you should use backticks (`). my $result = `matlab`; See Quote and Quote like Operators. Note this captures STDOUT and not STDERR.
    2. If you don't care about output, then system is probably the command you want. system('matlab');

    See MATLAB documentation for info on how to interact with MATLAB on the command line.

      Kenneth, The Perl code I put together: #!/fs/COTS/gnu/bin/AIX/perl open(MATLAB,"| /home/sis3ewh/run_matlab -nojvm"); use FileHandle; MATLAB->autoflush(1); print MATLAB "timer = struct('seconds',289567981,'nano',944567000,'raw',[],'eng',char('Initializing'));save timer;\n"; What I'm hoping for is the following when loaded into MATLAB: timer = seconds: 289567981 nano: 944567000 raw: [] eng: 'Initializing I get dumped into MATLAB but no .mat MATLAB appears anywhere with the name "timer.mat" and I can't figure out where the problem is. Maybe some Monks are x-matlab gurus! At any rate, any help would be appreciated - thanks!!
        First, please read Writeup Formatting Tips, since your lack of <code> tags seriously impedes the legibility of your post. As well, the liberal addition of some <p> tags to break up the text would be helpful.

        I can say there are no immediately clear errors in the posted code, but again I have not worked with MATLAB on the command line for quite some time. You do some things I would consider quite poor form, such as not testing for the success of your open, using bareword file handles and using 2-argument open, but these are not syntax errors.

        A potentially much cleaner solution is rather than piping into the executable is use your Perl script to explicitly generate a .m file, and then execute the .m using system or backticks. In this way, you will have an audit trail so you can figure out which language is giving you problems - I frequently take this autogeneration approach with auxilliary programs. Remember that Perl was originally designed for text processing and does it exceptionally well. Perhaps this will give you some ideas:

        #!/usr/bin/perl use strict; use warnings; open(my $matlab, '>', 'm_file.m'); print $matlab <<EOF; x=1; save timer; EOF system('matlab -nojvm -r m_file');
        Why aren't you putting your code in <CODE></CODE> tags?
Re: calling MATLAB from perl in order to create a .mat file
by toolic (Bishop) on Jan 19, 2010 at 16:35 UTC