Main Content

Run Custom Tasks on Project Files

Custom tasks are MATLAB® functions that allow you to perform a series of operations on project files.

Open the example to download the example project.

The MATLAB desktop shows the Project panel with the project open on the left, the Project toolstrip on top, the Command Window in the middle, and the Project Issues panel on the right.

Explore Existing Custom Tasks

This example project provides four custom tasks. In the Project panel, in the Project toolstrip, click Settings. Then, navigate to the Custom Tasks section.

The custom tasks are stored in the custom_tasks folder in the project.

Custom Tasks section in the Project Settings dialog box shows four custom tasks on the left and Create New Function and Add Existing Function buttons on the right.

To explore the content of a custom task, open it from the Project panel. For example, the custom task analyzeModelFiles analyzes the models and returns how many blocks each model contains.

To create new custom tasks, in the Settings dialog box, in the Custom Task section, click Create new Function. For more information, see Create and Manage Custom Tasks.

Run Custom Task

To run the analyzeModelFiles custom task on the project models, in the Project panel, multi-select the model files stored in the models folder. Then, right-click and select Run Custom Task > analyzeModelFiles.

The Custom Tasks dialog box prepopulates the Custom Task field with analyzeModelFiles.

  1. By default, after running the custom tasks on the project files you selected, the tool saves the results in an HTML report in the project root folder. You can change the report path, name and extension by using the Browse button or by pasting a path in the Report field.

  2. Click Run Task.

The results report opens automatically. Examine the Summary table in the report to ensure that the custom task ran correctly on all files you selected. To view detailed result information for a file, select the file in the table.

Edit an Existing Custom Task

You can edit custom tasks like any other MATLAB function. For example, modify the custom task analyzeModelFiles to programmatically add a label with data, as well as saving any dirty model files.

1. In the Command Window, run the following MATLAB code to create a Metrics category and a Block Count label in the project.

proj = currentProject;
category = createCategory(proj,'Metrics','double');

2. Double-click analyzeModelFiles.m to edit the file in the MATLAB Editor.

3. Add the following lines just after the sprintf command:

[~, compileStats] = sldiagnostics(name,'CompileStats');
addLabel(projectFile, 'Metrics','CPU Compile Time',sum([compileStats.Statistics.CPUTime]));

You can use the MATLAB Editor to set breakpoints and debug a custom task function, just as with any other MATLAB function.

If you rerun the modified custom task, the task adds the CPU Compile Time label to each model file that can be compiled, and attaches data to the label showing the total time for all compilation phases for the model. Models that cannot be compiled show Failed to analyze file in the results report, and details display as a warning in the Command Window. Examine the custom task analyzeModelFiles.m to see how to handle errors.

See Also

Perform Impact Analysis with a Project

Using a Project with Git