1

I'm working with a command line Matlab (i.e. remote ubuntu linux workstation) and am trying to execute a script.

When I ran:

matlab -nodesktop -nosplash -r "my_script.m"

matlab would open and give me an error message stating

Undefined variable "my_script" or class "my_script.m"

I found a thread on stackoverflow with a user having a similar problem: Matlab: Running an m-file from command-line

but when I attempted to implement the suggested syntax:

matlab -nodesktop -nosplash -r "run('my_script.m');"

I now get a syntax error,

Unexpected MATLAB expression.

Error in run (line 96)
evalin('caller', [script ';'])

I seldom use Matlab and even more rarely do so w/o a gui, so I've been trying without success to fix the syntax using information from online messageboards.

Community
  • 1
  • 1
Max
  • 487
  • 5
  • 19
  • Most likely my_script.m is not on your Matlab path, so it cannot find it. Add it to your path and/or switch current directory before trying to run the script. – nirvana-msu Jul 26 '16 at 21:37
  • I checked that with pwd in an open matlab session. The working directory is where my_script.m is located – Max Jul 26 '16 at 21:46

1 Answers1

6

You just need to remove .m extension:

matlab -nodesktop -nosplash -r "my_script"

The reason is that my_script.m is not a valid Matlab statement. In order to run a script / function you need to execute it using its name, i.e. my_script. You can see that if you try running my_script.m and my_script statements right from Matlab command window.

The second error you mention (when using run command) seems to be the actual error in your script. It seems like you forgot to copy-paste the very top line which should show the line number where the error occurs. What you see below, i.e. Error in run (line 96) and evalin('caller', [script ';']) is just a second entry in the stack trace. It does confirm the the error occurs while evaluating your script using evalin.

nirvana-msu
  • 3,877
  • 2
  • 19
  • 28
  • @Max also, note that this means "open a matlab session and run the script", not "run the script and exit". Your matlab session will remain open. If you just want to run the script and exit, then do `-r "myscript; exit"` – Tasos Papastylianou Jul 26 '16 at 22:22