LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2009, 10:42 PM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
interfacing MATLAB with shell


I must write several script-like tools in MATLAB (no choice here) which need to be run much like a shell script would. For example, the MATLAB code will need to take e.g. a group of images and perform some sort of transformation, mostly arithmetic and statistics on a per-pixel basis. I personally think the appropriate tool for this is R; however, I've been vetoed. The main reasons I'd prefer R are that it can serve as a script interpreter, the code could be run by non-scientists without spending thousands of dollars on MATLAB, and my MATLAB license only works when I'm on the university's network.

I need a consistent way to call a .m as if it were a script, i.e. passing it a set of arguments specifying e.g. the file to open and the operations to perform. I need to do this from a Unix shell script because it will be a part of a chain of preexisting C, csh, and bash tools. I'm not particularly familiar with MATLAB; however, I think I need to wrap it with a line something like this:
Code:
matlab -nodisplay -r "args='$*';script;quit;"
Unfortunately I can't test that right now (having license verification problems.) Thanks.
Kevin Barry
 
Old 05-24-2009, 12:10 AM   #2
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
How about this:

./m <<EOF
do_something
EOF
 
Old 05-24-2009, 01:58 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
The problem is that (as far as I know) MATLAB can't be a script interpreter. In other words, you can use #!/bin/bash, #!/usr/bin/Rscript, etc., which have built-in methods to pass arguments from the command line to the script; however, I can't think of a clean way to do that with MATLAB. Really the only thing I can think of is to wrap the MATLAB execution in bash (using either the method I gave or yours,) which would require the bash script to manually pass the arguments to the MATLAB script.
Code:
#!/bin/bash

#run as: > ./wrapper.sh [MATLAB script] (args...)

{
  echo -n 'args = ['; #<-- the MATLAB script would be written to expect this
  for argument in $*; do
    echo -n " '$argument'"
  done;
  echo ' ];';
  echo "$1";
  echo 'quit;';
} | matlab -nodisplay
Again, unable to test it, but you get the idea. Rscript is essentially the above for R, but I can't seem to find its counterpart for MATLAB. Thanks.
Kevin Barry
 
Old 05-24-2009, 03:12 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Yes. It should work. Just some notes:

1) store $1 in a shell variable then use shift to skip the first argument before doing the for loop, otherwise the name of the script will be the first element of the args array.

2) if the strings (arguments) have not the same length, you will get an error, unless you use a cell array. To retrieve a string from a cell array element the command is char.

Therefore, a slightly corrected version of your script is:
Code:
#!/bin/bash
(
  script=$1
  shift
  echo -n 'args = {';
  for argument in $*; do
    echo -n " '$argument'"
  done;
  echo ' };';
  echo "$script";
  echo 'quit';
) | matlab -nodisplay > matlab.out 2>&1
 
Old 05-24-2009, 12:59 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
1) store $1 in a shell variable then use shift to skip the first argument before doing the for loop, otherwise the name of the script will be the first element of the args array.
I can't believe I've never seen shift before! I was thinking the same thing but couldn't think of a good way to do it. I had a problem with spaces within arguments causing it to split, so maybe this (with my new toy shift):
Code:
{
  script="$1"
  shift
  echo -n 'args = [';
  while [ $# -ne 0 ]; do
    echo -n " '$1'"
    shift
  done;
  echo ' ];'
  echo "$script";
  echo 'quit;';
} | matlab -nodisplay &> matlab.out
I guess I have some other scripts to update now (I've been skipping arguments the hard way.) Thanks!
Kevin Barry

PS Would it be appropriate to do this?
Code:
  echo "cd('`dirname "$script"`');"
  basename "$script"

Last edited by ta0kira; 05-24-2009 at 01:05 PM.
 
Old 05-26-2009, 10:37 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
I finally got to test it. I had to change a few things, but now it works:
Code:
#!/bin/bash

if [ $# -eq 0 ]; then
  echo "$0 [script(.m)] (args...)" 1>&2
  echo "$0: the current path will be stored in 'pwd'" 1>&2
  echo "$0: arguments will be stored in matrix 'args'" 1>&2
  echo "$0: print output data to 'script_out'" 1>&2
  exit 1
fi


{
  script="$1"
  shift
  echo "pwd = '`pwd`';"
  echo -n 'args = {';
  while [ $# -ne 0 ]; do
    echo -n " '$1'"
    shift
  done;
  echo ' };'
  echo "cd('`dirname "$script"`');"
  echo "script_out = fopen('/proc/self/fd/2','w');";
  basename "$script"
  echo 'quit;';
} | `which matlab` -nodisplay -nojvm 3>&2 2>&1 1>&3
Code:
% ~/test.m

fprintf(script_out, 'path: %s\n', pwd);
fprintf(script_out, 'arg:  %s\n', args{:});
Code:
matlab.sh ~/test one two three 'and four' 2> /dev/null
Kevin Barry
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB interfacing ramesh6056 Linux - Hardware 2 05-20-2007 09:44 PM
AX.25 interfacing alek66 Linux - Hardware 0 12-01-2006 10:19 AM
Interfacing with devices worldgnat Programming 9 07-18-2006 12:35 AM
Terminal cmd for starting Matlab M-file editor without starting matlab fubzot Linux - Software 2 02-15-2005 07:49 AM
interfacing TI-83 1337 Twinkie Programming 2 10-15-2004 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration