MATLAB: How to run the MATLAB task or application in the background on a set schedule in Windows

MATLAB

I would like to be able to generate a MATLAB program which will be running in the background of my operating system, asking user some questions every so often. I want it to be able to run even though MATLAB is closed.

Best Answer

There are three common approaches to running a MATLAB task once a day, once a week, or on a similar schedule on WIndows:
A. MATLAB only - This system is analogous to the CRON utility on Linux/UNIX/Mac.
Scheduling a MATLAB job with Windows Task Scheduler is similar to invoking a MATLAB script from the Windows system prompt. We will use the command ‘matlab –r “<scriptname>”’ to start MATLAB and immediately execute a script.
 
1.    Create a MATLAB script that you would like to run as a scheduled job. In this example, I will call my script example.m.
2.    Move the script to the MATLAB Startup Folder. To get the location of this folder, run the following command in MATLAB:
>> userpath
3.     Open Windows Task Scheduler.
4.    Under “Actions”, select “Create Basic Task”. This will open a new dialog window.
5.    On the “Create a Basic Task page”, fill out a name and description for the task, and click “Next”.
6.    On the “Trigger” page, select timing options for the task, and click “Next”.
7.    On the “Actions” page, select “Start a program”.
8.    On the “Start a Program” page, fill out the following fields:
       Program/Script: matlab
       Add arguments (optional): -r “example”
9.    Click “Finish”.
B. MATLAB Compiler - create a standalone application which will do the operation once. Schedule it to run as in option 1.
C. MATLAB Compiler - create a standalone application which has a loop or timers to do the operation when desired. This application would run continuously.
If you take this approach, be aware of an issue with your version of MATLAB, which causes the applications to exit while waiting to execute timer tasks.
This is not an exhaustive list. Other options include building Windows Service that calls a MATLAB Compiler-generated DLL shared library, COM Object, .NET Assembly, or Java component.