0

I am trying to start Matlab and run a script scheduled at a specific time using the windows Task Scheduler.

If I use a scheduled task I can see Matlab starting, but this last fails to load the script and returns me the error below

??? Unexpected Matlab operator.

Do you know what it is and why?

I am using the following syntax

c:\app\matlab\bin\matlab.exe -r c:\MyURL\ScriptFile.m

If I load the script manually and run it it tells me that the file is not in the path so give ms a choice between

  • Change Current Directory
  • Add Folder to the Path

Either choices are fine and the script runs fine.

Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173

1 Answers1

3

Matlab is starting in its main directory and -r requires your function to be in quotation marks, thats why you get the error.

And you need to change to your workspace first, the syntax is as follows:

matlab -sd pathToYourWorkspace -r "function(parameters)"

Maybe you also want to avoid the complete loading of the whole Matlab working environment, so add at the end:

-nodesktop -nosplash

If you run your task sheduled, are you doing it multiple times? Are you aware that every function call like above opens a new instance of Matlab? This question may be helpful then.


From the comments: of course you could just use the command run to call a script wherever it is.

"run('c:\MyURL\ScriptFile.m')" is an example for "functionName(YourArgs)"

as run is a function and the string 'c:\MyURL\ScriptFile.m' its argument. In this case it is usually not necessary to change the workspace before.

Community
  • 1
  • 1
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • 1
    Your suggestion is correct - but the cause of the problem is actually that the "-r" argument is passed directly to MATLAB to be evaluated - so the error is what you'd see if you typed `c:\MyURL\ScriptFile.m` at the MATLAB `>>` prompt. – Edric Jan 09 '14 at 16:22
  • Hi. To run a script file I called -r "run('c:\MyURL\ScriptFile.m')" while to call a function just call -r "functionName" or , if parameters are needed -r "functionName(YourArgs)". – Abruzzo Forte e Gentile Jan 13 '14 at 15:04
  • @AbruzzoForteeGentile: `"run('c:\MyURL\ScriptFile.m')"` is an example for `"functionName(YourArgs)"` as `run` is a function an the string `'c:\MyURL\ScriptFile.m'` its argument ;) – Robert Seifert Jan 13 '14 at 18:40