Running a matlab script in command line (misleading .m)

The syntax to run a matlab script in batch mode from command line is:

matlab -nodesktop -nosplash -r [command]

Since I’ve accostumed to pass file name as argument to several unix tools I made some confusion on execute a script in matlab. In matlab we should pass a command, not a file name as an argument. So for run a script from command line you should first ensure that your script is in malab path (or is in your current path). Then, remember that to make a call of a Matlab script with several commands without a function you just call it by the script file name (without the .m). So if your script name is foo.m you should call as follow:

matlab -nodesktop -nosplash -r foo

See ya

8 Responses to Running a matlab script in command line (misleading .m)

  1. Andrey says:

    Is it possible to avoid opening a matlab command window and see script output in windows shell command window?

    • emanuelvianna says:

      Hi Andrey,

      Yes, you just need to call your script (without.m) with -nodesktop (which will open matlab in shell instead of in a windows gui) and -nosplash (to don’t show matlab logo when it is loading) arguments (works in linux).

      Best regards,
      Emanuel

  2. piyush says:

    how to pass argument also to the same file?

    • emanuelvianna says:

      in the version of matlab (7) that i use that is not possible, the parameter “foo” that you suplly in call “matlab -nodesktop -nosplash -r foo” is a script name (which may have a function with the same name), bit it is not a function, so you can’t parametrize

  3. Ian says:

    Hi there, I have seen tips like these around and they never work for me at all. I am using Putty to SSH to my research group’s node, and the only thing that works for me is
    matlab -nodisplay < mtest.m this displays the output for me. Warning, make sure you use the left caret "” it output’s matlab’s “text splash” (note not java splash page) and over writes the file.

    In this case it does not matter the file format descriptor, but if the file has one it must be specified. info from http://sys-log.bencane.com/2012/04/unix-shell-the-art-of-io-redirection/

  4. Ian says:

    whoa trying to type the caret’s <<<>>>>> why is that difficult for this interface to display?

    • emanuelvianna says:

      Some interfaces are not so user friendly =/
      Did it work what you are testing?

      Best regards,
      Emanuel

  5. Neha says:

    Thank you very much. This is going to help me a lot. 🙂

Leave a comment