Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Running Custom Tasks with a Simulink Project

This example shows how to apply a custom task to a set of files managed by Simulink® Project. The example custom task analyzes the Simulink models which are in the project, reporting the number of blocks in each model.

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.

Simulink.ModelManagement.Project.projectDemo('airframe');
rebuild_s_functions('no_progress_dialog');
Starting: Simulink
Initializing: Project Path
Identifying shadowed project files
Running: /tmp/publish_examples1/bml.batserve.057841/work/projects/slexamples/airframe/utilities/set_up_project.m
Building with 'gcc'.
MEX completed successfully.

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

The Simulink Project Tool opens and loads the project.

Select a Custom Task

2. Click the Custom Task button in the Tools section of the Simulink® Project toolstrip tab.

You define a custom task with a MATLAB® function. The example Airframe project contains example custom tasks in the batch_jobs folder.

3. The "Custom Task" drop down menu lists available custom tasks.

The drop down menu contains all shortcuts that have an action type of "Custom Task". You can set the action type of a shortcut at the Shortcuts node.

4. From the 'Utility' section of the menu, select the 'Analyze Model Files' custom task.

The function name of your selected custom task appears in the "Custom task" edit box. 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 will count the number of blocks in each model and attach this number to the label.

Select Files to Include in the Custom Task

5. In the Custom Task dialog box, verify that all model files have selected check boxes in the Include column.

Running a Custom Task

6. Click the Run Custom Task button on the bottom right of the Custom Task overlay.

The results for a selected file are also shown in the Results pane at the bottom of the dialog box. This can be useful when the results returned 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 Actions button at the top right.

Editing an Existing Custom Task

Custom Tasks are MATLAB functions. Edit your custom task with the MATLAB editor. In the following steps, you modify the custom task to use the Project API to 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 = simulinkproject;
category = createCategory(project, 'Metrics', 'double');

8. Double-click analyzeModelFiles.m (or right-click and select Open) 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.

Creating a New Custom Task

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.

The Custom Task dialog box provides a Create button to help you create a custom task with the correct function signature.

10. Click the Create button in the bottom right of the custom task dialog box.

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 'batch_jobs' 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.

Further Information

Simulink Project documentation

Was this topic helpful?