Main Content

Create Custom Tasks

Custom tasks are MATLAB® functions that allow you to perform a series of operations on one or more files. You can create a custom task function and then run the custom task on a select set of files in your project. For example, you can create a custom task to check all the code files for errors or to run all the tests in the project.

Create a Custom Task Function

To create a custom task function:

  1. In the Project tab, click Custom Tasks and then select Manage Custom Tasks. The Manage Custom Tasks dialog box opens.

  2. Click Add and then select Add Using New Function. If you want to add an existing script as a custom task, select Add Using Existing Function.

  3. Specify a file name for the script and save the new file on the MATLAB path. The MATLAB Editor opens the new file containing an example custom task function.

  4. Edit the function to perform the desired action on each file. Use the instructions at the top of the file to guide you to create a custom task with the correct function signature. Your custom tasks must accept a full path to a file as the single input argument and return a single output argument.

    For example, this custom task function extracts Code Analyzer information for each file using the checkcode function.

    [~,~,ext] = fileparts(file);
    switch ext
        case {'.m', '.mlx', '.mlapp'}
            result = checkcode(file, '-string');
            result = [];

  5. Save the file.

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

Run a Custom Task

To run a custom task on a select set of files in your project:

  1. On the Project tab, click Custom Tasks and then select Run Custom Task.

  2. In the Include column of the table, select which project files you want to run the custom task on.

    To include or exclude multiple files in the table at once, press the Shift or Ctrl key, select the files, and then right-click and select Include or Exclude. If the custom task function can identify the files to operate on, include all files.

  3. In the Custom task field, select from the available custom task functions. You also can enter the name of a task directly in the field, or click Browse.

  4. Click Run Task to run the task. The Custom Task Report window displays the results.

  5. Examine the Results column in the table to ensure that the custom task ran correctly on all files. To view detailed result information for a file, select the file in the table. The results pane at the bottom of the Custom Task Report displays the details.

Save Custom Task Report

Saving a Custom Task Report is useful if you want to save a record of the custom task results, or if you want to share the results with others.

To save the Custom Task Report, click the Publish Report button at the bottom of the Custom Task Report. You can either save the report as an HTML file or a Microsoft® Word file. If you have MATLAB Report Generator™, you also can save the report as a PDF file.

To see the report file and add it to your project, switch to the All files view.

Related Topics