Running Batch Jobs with a Simulink Project

This example shows how to apply a batch job function to a set of files managed by Simulink® Project. The example batch job function identifies and saves any loaded Simulink model files that contain unsaved changes. This can be a useful action to perform before committing changes to source control.

Setting Up the Example Files and Opening Simulink Project

1. Run the following commands to create and open a working copy of the "sldemo_slproject_airframe" example.

Initializing: Project Path
Identifying shadowed project files
Running: /tmp/BR2015bd_259410_35349/slexamples/airframe3/utilities/set_up_project.m
Loading: /tmp/BR2015bd_259410_35349/slexamples/airframe3/data/buses.mat
Loading: /tmp/BR2015bd_259410_35349/slexamples/airframe3/data/f14_digital_data.mat
Building with 'gcc'.
MEX completed successfully.

The project example copies files to your temporary folder so that you can edit them and put them under local version control.

The Simulink Project Tool opens and loads the project already under version control.

Select a Batch Job Function

2. Click the Batch Job node in the tree.

You define a batch job with a MATLAB® function. The example Airframe project contains example batch job functions in the batch_jobs folder.

3. Next to the "Batch function" edit box, click Select to open a menu of available batch jobs.

The menu contains all shortcuts that have an action type of "Batch Job". You can set the action type of a shortcut at the Shortcuts node.

4. Navigate to the 'Utility' submenu and select the 'Save Model Files' batch job.

The function name of your selected batch job appears in the "Batch function" edit box. The example saveModelFiles batch job saves any loaded Simulink model files that contain unsaved changes. Checking for unsaved changes can be a useful action to perform before committing changes to source control.

Select Files to Include in the Batch Job

5. Modify a model before running the batch job. This batch job checks for and saves only Simulink model files marked dirty. Right-click a model file and select Open, then make a change. A small change such as moving a block is enough to try this batch job.

6. In the Batch Job view, verify that all model files have selected check boxes in the Include column.

Running a Batch Job

7. Click the Run Batch Job button on the bottom right of the Batch Job view.

The batch job function saves any models with unsaved changes, and displays in the Results column which models have been saved. The results for a selected file are also shown in the Results panel at the bottom of the view. This can be useful when the results returned are long, or contain HTML markup.

The following example shows the view after running the batch job on some models. You can customize the columns to show with the "cog" icon Actions button at the top right.

Editing an Existing Batch Job

Batch jobs are MATLAB functions. Edit your batch jobs with the MATLAB editor. In the following steps, you modify the batch job to use the Project API to add a To Review label, as well as saving any dirty model files.

8. Run the following MATLAB code to create a Review category and a To Review label in the project.

project = simulinkproject;
category = createCategory(project, 'Review');
createLabel(category, 'To Review');

9. Double-click saveModelFiles.m (or right-click and select Open) to edit it in the MATLAB editor.

10. Add the following lines just after the save_system command:

project = simulinkproject;
projectFile = findFile(project, file);
addLabel(projectFile, 'Review', 'To Review');

If you modify some model files and rerun the batch job, the batch job adds the To Review label to the modified files.

You can use the MATLAB editor to set breakpoints and debug a batch job function, just as with any other MATLAB function.

Creating a New Batch Job

Create a new batch job by creating a new MATLAB function. Your batch job functions must:

  • Be saved on the MATLAB path.

  • Accept a single input argument: a full path to a file.

  • Return a single output argument.

The Batch Job view provides a Create button to help you create a batch job with the correct function signature.

11. Click the Create button in the bottom right of the batch job view.

A file dialog opens asking you to choose where to create the new batch job. The batch job must be saved on the MATLAB path to run.

12. Provide a file name and save the file in the 'batch_jobs' folder within the project.

The MATLAB editor opens the file pre-populated with a simple example batch job.

13. To create your new batch job, edit the contents of the example batch job function and save.

Further Information

Simulink Project documentation

Was this topic helpful?