This example shows how to apply a custom task to a set of files managed by project. The example custom task analyzes the Simulink models which are in the project and reports the number of blocks in each model.
1. Create and open a working copy of the project example files. MATLAB® copies the files to an example folder so that you can edit them.
Building with 'MinGW64 Compiler (C)'. MEX completed successfully.
2. On the Project tab, click the down arrow to expand the Tools gallery. Under Project Checks, click Custom Tasks.
You define a custom task with a MATLAB® function. The example Airframe project contains example custom tasks in the custom_tasks folder.
3. The Custom task menu lists available custom tasks.
To view, edit, and create custom tasks, on the Project tab, click the down arrow to expand the Tools gallery. Under Project Checks, click Custom Tasks. In the Custom Task dialog box, click Manage.
4. Select the Analyze Model Files custom task.
The function name of your selected custom task appears in the Custom task field. The example analyzeModelFiles adds a label from the category Metrics to each model file in the project. Labels in this category have numerical data. The custom task counts the number of blocks in each model and attaches this number to the label.
5. In the Custom Task dialog box, in the Include column, verify that all the model files check boxes are selected.
6. To run the custom task, click Run Task.
The results for a selected file are shown in the Results pane at the bottom of the dialog box. This can be useful when the returned results are long, or contain HTML markup.
The following example shows the dialog box after running the custom task on some models. You can customize the columns to show with the cog icon button .
Custom Tasks are MATLAB functions. Edit your custom task with the MATLAB Editor. For example, modify the custom task to programmatically add a label with data, as well as saving any dirty model files.
7. Run the following MATLAB code to create a Metrics category and a Block Count label in the project.
project = currentProject; category = createCategory(project,'Metrics','double');
8. Double-click analyzeModelFiles.m to edit it in the MATLAB Editor.
9. 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 custom task, it 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 Custom Task Report, and details display as a warning in the Command Window. Examine the custom task analyzeModelFiles.m to see how to handle errors.
To view the new metrics data, either show the Metrics column in the Custom Task Report, or look in the project Files view.
Create a new custom task by creating a new MATLAB function. Your custom tasks must:
Be saved on the MATLAB path.
Accept a single input argument: a full path to a file.
Return a single output argument.
To create custom tasks, on the Project tab, click the down arrow to expand the Tools gallery. Under Project Checks, click Custom Tasks. In the Custom Task dialog box, click Manage. In the Manage Custom Tasks dialog box, click Add to open a new file with instructions that guide you to create a custom task with the correct function signature.
10. Select Add>Add Using New Script.
A file dialog opens asking you to choose where to create the new custom task. The custom task must be saved on the MATLAB path to run.
11. Provide a file name and save the file in the custom_tasks folder within the project.
The MATLAB editor opens the file pre-populated with a simple example custom task.
12. To create your new custom task, edit the contents of the example custom task function and save.