shlomosh
shlomosh

Reputation: 79

How to run matlab code in linux as script file?

I am looking into running matlab script in Linux similar to bash/python scripts. I.e., a matlab script that can be run as an application.

Upvotes: 1

Views: 895

Answers (2)

Isaac
Isaac

Reputation: 3616

You can get a similar effect without your custom mash script by adding the following header to the files you want to be executable:

#/usr/bin/bash
/path/to/matlab -r "$(sed -n -e '4,$p' < "$0")"
exit $?

If you want matlab to terminate after executing the script, as in your example, you could replace the second line with

sed -n -e '4,$p' < "$0" | /path/to/matlab

The idea here is to execute a bash command that simply clips off the header of the script, and passes the rest along to matlab.

Upvotes: 1

shlomosh
shlomosh

Reputation: 79

Here is the implementation I came up with -

  • Create /usr/bin/mash script file containing the following lines -

    #!/bin/bash
    
    grep -ve '^(#!\|^\s*$)' ${@: -1} | ${@: 1:$#-1}     
    exit $?
    
  • Make mash script executable -

    $ chmod +x /usr/bin/mash
    
  • Write matlab script file called test.msh

    #!/usr/bin/mash /usr/local/MATLAB/R2012a/bin/matlab -nodisplay
    
    format long
    
    a = 2*pi           % matlab commands ...
    
  • Make test.msh script executable -

    $ chmod +x mash
    
  • Run test.msh

    $ ./test.msh 
    
    ...
    
    >> >>  a =
    
    6.283185307179586
    

Upvotes: 0

Related Questions