Thursday, June 5, 2025

Windows: Using Operating System Scheduler

  1. Create a MATLAB Script: Save your MATLAB code in a .m file, e.g., myScript.m:
  2. 1
    2
    3
    disp('Running my daily task at 6 PM!');
    % Your code here
    exit; % Close MATLAB after execution
    
  3. Create a Batch File: Create a .bat file (e.g., run_matlab_script.bat) to run the script in MATLAB:
  4. 1
    "C:\Program Files\MATLAB\R2023a\bin\matlab.exe" -batch "run('C:\path\to\myScript.m')"
    • Replace R2023a with your MATLAB version.
    • Replace C:\path\to\myScript.m with the full path to your script.
  5. Schedule with Windows Task Scheduler:
    • Open Task Scheduler (search for it in Windows).
    • Click Create Task (not Basic Task for daily scheduling).
    • General Tab:
      • Name the task (e.g., "Run MATLAB Daily").
      • Check "Run whether user is logged on or not" for reliability.
    • Triggers Tab:
      • Click New → Set trigger to Daily at 6:00 PM.
      • Ensure "Enabled" is checked.
    • Actions Tab:
      • Click New → Action: Start a Program.
      • Browse to your .bat file (e.g., C:\path\to\run_matlab_script.bat).
    • Conditions/Settings:
      • Optional: Set conditions like "Start only if computer is on AC power."
      • Save the task, entering your user credentials if prompted.
  6. Test:
    • Right-click the task in Task Scheduler and select Run to verify it executes correctly.

No comments:

Post a Comment