How do I execute a MATLAB script at a specific time each day in Windows?

161 views (last 30 days)
I tried to schedule a task to run a MATLAB script at a specific time. I went to:
Start -> Programs -> Accessories -> System tools -> Scheduled Tasks
However, this just opens the MATLAB file, does not run it.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Jan 2023
Edited: MathWorks Support Team on 24 Jan 2023
Schedule a MATLAB task in Windows Operating System prior to Windows 7
  1. Click on Start -> Programs -> Accessories -> System Tools -> Scheduled Tasks -> Add Scheduled Task
  2. Choose MATLAB.
  3. After you finish setting up the scheduled task, open the task's "Properties".
  4. If  under "Run" : 
    D:\MATLAB.exe
    is specified, change it to:
    D:\MATLAB.exe -r mfile [-logfile C:\logfile]
    where "mfile" is the name of the MATLAB file you want to execute. If you use the option "-logfile", then any MATLAB Command Window output will be saved in "C:\logfile". You can also define multiple commands, separated by commas or semi-columns. For example:
    D:\MATLAB.exe -r cd('C:\'), mytest, exit -logfile C:\logfile
    would change the current directory to "C:\" (where the MATLAB file "mytest.m" assumably lives), run "mytest.m" and exit MATLAB. The MATLAB Command Window output will be saved in "C:\logfile".
Schedule a MATLAB task in Windows Operating System running Windows 7 or newer
  1. Click on Start -> Task Scheduler
  2. In Task Scheduler, click on "Create Task".
  3. In "General" tab set the "Name" field to the name of the task.
  4.  In the "Triggers" tab click "New" to select the desired trigger time and frequency of the task. Click "Ok" to save the task.
  5.  In the "Actions" tab, click "New". The "Action" should be kept as "Start a program".
  6.  For "Program/script", use "Browse..." to find the MATLAB executable, which should be a value like:
    C:\Program Files\MATLAB\R2011b\bin\matlab.exe
  7. Set arguments to:
    -r scriptname; quit
    It is not required to specify the ".M" extension here.
  8. Set the "Start in" value as the directory containing the script file, e.g.:
    C:\users\username\Desktop\Folder
     Click "Ok" to save your changes.
  9. Click "Ok" to finish and save.
  1 Comment
Angelo Yeo
Angelo Yeo on 12 Oct 2023
Edited: Angelo Yeo on 2 Nov 2023
A side note: You cannot use double quote as input arguments for scriptname in step 7 for Task Schdeuler of Win7 or newer. For example, let's say a function is defined in this way:
function strOutput1 = myfun(strInput1, strInput2)
strOutput1 = strInput1 + strInput2;
end
And you want to use the function in the following way:
myfun("foo","bar")
You may want to set the "argument" in the following way, but this would not run the script as expected.
-r myfun("foo","bar");quit
This is because the whole script would be wrapped with double quotes internally.
As a workaround, you need to use single quote as input arguments and change them inside the MATLAB functions.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!