Automate Project Management Tasks

Manipulate a Simulink Project at the Command Line

You can automate some Simulink® project tasks using scripts. You can manipulate project files and labels and use commands for scripting operations on project files. This example shows how to use commands to automate Simulink project tasks with files, including working with modified files, dependencies, shortcuts, and labels.

To automate startup and shutdown tasks, see Automate Startup Tasks with Shortcuts.

Get Simulink Project at the Command Line

This example shows how to open the Airframe project and use simulinkproject to get a project object to manipulate the project at the command line. You must open a project in Simulink Project to perform command-line operations on the project.

proj = simulinkproject
proj = 

  ProjectManager with properties:

        Information: [1×1 slproject.Information]
       Dependencies: [1×1 slproject.Dependencies]
          Shortcuts: [1×9 slproject.Shortcut]
        ProjectPath: [1×7 slproject.PathFolder]
  ProjectReferences: [1×0 slproject.ProjectReference]
         Categories: [1×1 slproject.Category]
              Files: [1×33 slproject.ProjectFile]
         RootFolder: 'C:\slexamples\airframe'

Find Project Commands

This example shows how to find out what you can do with your project.

Examine project commands.

Methods for class slproject.ProjectManager:

addFile                       createCategory  
addFolderIncludingChildFiles  export
close                         findCategory

findFile                      refreshSourceControl
isLoaded                      reload 
listModifiedFiles             removeCategory


Examine Project Files

After you get a project object, you can examine project properties.

  1. Examine the project files.

    files = proj.Files
    files = 
      1x33 ProjectFile array with properties:
  2. Use indexing to access files in this list. The following command gets file number 13. Each file has two properties describing its path and attached labels.

    ans = 
      ProjectFile with properties:
          Path: 'C:\slexamples\airframe\models\AnalogControl.mdl'
                     Labels: [1x1 slproject.Label]
                   Revision: '2'
        SourceControlStatus: Unmodified
  3. Examine the labels of the 13th file.

    ans = 
      Label with properties:
                File: 'C:\slexamples\airframe\models\AnalogControl.mdl'
            DataType: 'none'
                Data: []
                Name: 'Design'
        CategoryName: 'Classification'
  4. Get a particular file by name.

    myfile = findFile(proj, 'models/AnalogControl.mdl')
    myfile = 
      ProjectFile with properties:
                    Path: 'C:\slexamples\airframe\models\AnalogControl.mdl'
                     Labels: [1x1 slproject.Label]
                   Revision: '2'
        SourceControlStatus: Unmodified

  5. Find out what you can do with the file.

    Methods for class slproject.ProjectFile:
    addLabel     findLabel    removeLabel

Get a List of Modified Files

  1. Modify a project model file by adding an arbitrary block.

    add_block('built-in/SubSystem', 'AnalogControl/test')
  2. Get all the modified files in the project.

    modifiedfiles = listModifiedFiles(proj)
    modifiedfiles = 
      1x2 ProjectFile array with properties:

    Observe two modified files. Compare with the Modified Files view in Simulink Project, where you can see a modified model file, and the corresponding .SimulinkProject definition file.

  3. Get the second modified file.

    ans = 
      ProjectFile with properties:
                       Path: 'C:\slexamples\airframe\models\AnalogControl.mdl'
                     Labels: [1x1 slproject.Label]
                   Revision: '2'
        SourceControlStatus: Modified

    Observe the file SourceControlStatus property is Modified. Similarly, listModifiedFiles returns any files that are added, conflicted, deleted, etc., that show up in the Modified Files view in Simulink Project.

  4. Get all the project files with a particular source control status. For example, get the files that are Unmodified.

    proj.Files(ismember([proj.Files.SourceControlStatus], matlab.sourcecontrol.Status.Unmodified))
    ans = 
      1x32 ProjectFile array with properties:

Get File Dependencies

  1. Update the file dependencies.


    The project runs a dependency analysis to update the known dependencies between project files.

  2. Examine the dependencies in the project object.

    ans = 
      Dependencies with properties:
        Graph: [1×1 digraph]

    The dependencies property of the project object contains the graph of dependencies between project files in a MATLAB® digraph object. You can view the same dependency analysis graph in the Impact view of Simulink Project. If you want to plot the graph, instead of working with the digraph object, save the impact view to an image file. See Perform Impact Analysis.

  3. Get the graph dependencies.

    g = proj.Dependencies.Graph
    g = 
      digraph with properties:
        Edges: [22×1 table]
        Nodes: [24×1 table]

    You can use digraph methods to get information from the graph.

  4. Get the files required by a model.

    requiredFiles = bfsearch(g, which('AnalogControl'))
    requiredFiles =
      3×1 cell array
  5. Get the top-level files of all types in the graph.

    top = g.Nodes.Name(indegree(g)==0);
  6. Get the top-level files that have dependencies.

    top = g.Nodes.Name(indegree(g)==0 & outdegree(g)>0)
    top =
      5×1 cell array
  7. Find impacted (or "upstream") files by creating a graph from the transposed adjacency matrix.

    transposed = digraph(adjacency(g)', g.Nodes.Name);
    impacted = bfsearch(transposed, which('vertical_channel'))
    impacted =
      4×1 cell array
  8. Get information on your files, such as the number of dependencies and orphans.

    averageNumDependencies = mean(outdegree(g));
    numberOfOrphans = sum(indegree(g)+outdegree(g)==0);
  9. If you want to make changes to a model hierarchy, starting from the bottom up, find the topological order.

    ordered = g.Nodes.Name(flip(toposort(g)));

Query Shortcuts

  1. Examine the project's Shortcuts property.

    shortcuts = proj.Shortcuts
    shortcuts = 
      1x9 Shortcut array with properties:
  2. Examine the 8th shortcut in the array.

    ans = 
      Shortcut with properties:
                 File: 'C:\slexamples\airframe\utilities\set_up_project.m'
         RunAtStartup: 1
        RunAtShutdown: 0

    The RunAtStartup property is set to 1, so this shortcut file is set to run at project startup. At the command line, you can view but not change the RunAtStartup and RunAtShutdown properties. To set these properties, use the Shortcut Management node in the Simulink Project.

  3. Get the file path of a shortcut.

    ans =
  4. Examine all the files in the shortcuts cell array.

    ans = 
  5. Create a logical array that shows the shortcuts set to run at startup.

    idx = [shortcuts.RunAtStartup]
    idx =
     0   0   0   0   0   0   0   0   1
  6. Use the logical array to get only the startup shortcuts.

    startupshortcuts = shortcuts(idx)
    startupshortcuts = 
         Shortcut with properties:
                 File: 'C:\slexamples\airframe\utilities\set_up_project.m'
         RunAtStartup: 1
        RunAtShutdown: 0
  7. Get the path of the startup shortcut by accessing the File property.

    ans =
      1×51 char array

Label Files

Create New Category of Project Labels

  1. Create a new category of labels, of type char.

    createCategory(proj, 'Engineers', 'char')
    ans = 
      Category with properties:
                    Name: 'Engineers'
            SingleValued: 0
                DataType: 'char'
        LabelDefinitions: [1×0 slproject.LabelDefinition]

    In Simulink Project, the new Engineers category appears in the Labels pane.

  2. Find out what you can do with the new category.

    category = findCategory(proj, 'Engineers');
    Methods for class slproject.Category:
    createLabel  findLabel    removeLabel                       

Define a New Label

  1. Define a new label in the new category.

    createLabel(category, 'Bob');
  2. Get a label definition.

    ld = findLabel(category, 'Bob')
    ld = 
      LabelDefinition with properties:
                Name: 'Bob'
        CategoryName: 'Engineers'

Label a Project File

  • Attach a label to the retrieved file, myfile.

     addLabel(myfile, 'Engineers', 'Bob');
    If you select the file in Simulink Project, you can see this label in the label editor pane.

Attach Data to a Label

  • Get a particular label and attach data to it, for example, the text Please assess.

    label = findLabel(myfile, 'Engineers', 'Bob');
    label.Data = 'Please assess'
    label = 
      Label with properties:
                File: 'C:\slexamples\airframe\models\AnalogControl.mdl'
            DataType: 'char'
                Data: 'Please assess'
                Name: 'Bob'
        CategoryName: 'Engineers'

    You can specify a variable for the label data: mydata = label.Data.

Attach New Label with Numeric Data Type

  1. Create label category with numeric data type.

    createCategory(proj, 'Assessors', 'double');
    category = findCategory(proj, 'Assessors');
  2. Create new label.

    createLabel(category, 'Sam');
  3. Specify project file.

    myfile = proj.Files(14);
  4. Attach label to file and assign data value 2 to the label.

    addLabel(myfile, 'Assessors', 'Sam', 2)
    ans = 
      Label with properties:
                File: 'C:\slexamples\airframe\models\AnalogControl.mdl'
            DataType: 'double'
                Data: 2
                Name: 'Sam'
        CategoryName: 'Assessors'

Close Project

  • close(proj)

    Closing the project at the command line is the same as closing the project using the Simulink Project tool. For example, the project runs shutdown scripts.

More Project API Examples Including Source Control

For more examples, see:

Related Examples

Was this topic helpful?