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

170 次查看(过去 30 天)
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.

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2023-1-24
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 个评论
Angelo Yeo
Angelo Yeo 2023-10-12
编辑:Angelo Yeo 2023-11-2
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.

请先登录,再进行评论。

更多回答(1 个)

Tharikaa Ramesh Kumar
编辑:Tharikaa Ramesh Kumar 2024-6-11
Do you run time-consuming tasks on MATLAB and Simulink? Do you wish you could schedule them and run them in the background, unattended? How do you solve this question today?
MATLAB Product team is very interested to talk to you and learn from your experience. Please participate in this brief survey (6 questions).

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by