1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Better solution than what I came up with. Thanks!
0

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
    

2 Comments

Don't put it is /usr/bin; /usr/local/bin is a better place. /usr/bin is distribution-managed folder (or should be).
When you have code blocks inside lists, you need 8 indentation instead of 4 ;).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.