Documentation Center

  • Trial Software
  • Product Updates

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 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 the Simulink Project to perform command-line operations on it.

sldemo_slproject_airframe
proj = simulinkproject
proj = 

  ProjectManager with properties:

          Name: 'Simulink Project Airframe Example'
    Categories: [1x2 slproject.Category]
Shortcuts: [1x6 slproject.Shortcut]
         Files: [1x24 slproject.ProjectFile]
    RootFolder: [1x61 char]

Find Project Commands

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

Examine project commands.

methods(proj)
Methods for class slproject.ProjectManager:

addFile createCategory  findCategory
isLoaded removeCategory close
export findFile reload removeFile

Examine Project Files

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

  1. Examine the project files.

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

    proj.Files(8)
    ans = 
    
      ProjectFile with properties:
    
          Path: 'C:\Temp\project1\airframe\models\AnalogControl.mdl'
        Labels: [1x1 slproject.Label]
  3. Examine the labels of the eighth file.

    proj.Files(8).Labels
    
    ans = 
    
      Label with properties:
    
    File: 'C:\Temp\project1\airframe\models\AnalogControl.mdl'
                Data: []
            DataType: 'none'
                Name: 'Design'
        CategoryName: 'Classification'
  4. Get a particular file by name.

    myfile = findFile(proj, 'models/AnalogControl.mdl')
    myfile = 
    
      ProjectFile with properties:
    
          Path: [1x86 char]
        Labels: [1x1 slproject.Label]
    

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

    methods(myfile)
    
    Methods for class slproject.ProjectFile:
    
    addLabel
    findLabel
    removeLabel
    

Label a Project File

  • Attach a label to the retrieved file, myfile.

    addLabel(myfile, 'Review', 'To Review')

    This label appears next to the file in the Simulink Project.

Attach Data to a Label

  1. Get a particular label and attach data (the string Code reviewer: Bob) to it.

    label = findLabel(myfile, 'Review', 'To Review');
    label.Data = 'Code reviewer: Bob'
    label = 
    
      Label with properties:
    
                File: [1x86 char]
                Data: 'Code reviewer: Bob'
            DataType: 'char'
                Name: 'To Review'
        CategoryName: 'Review'
    

    Alternatively, you can specify a variable for the label data: mydata = label.Data.

  2. Select the file AnalogControl in the Simulink Project. Expand the lower pane, Labels attached to AnalogControl.mdl, and select the label To Review.

    The label data Code reviewer: Bob appears in the Value pane. You can edit label data in this window and click Apply to save label data changes.

Create New Category of Project Labels

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

    createCategory(proj, 'Engineers', 'double')
    category = 
    
      Category with properties:
    
                    Name: 'Engineers'
                DataType: 'double'
        LabelDefinitions: [ ]
  2. Find out what you can do with the new category.

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

Define a New Label

  1. Define a new label in the new category.

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

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

Attach New Labels and Label Data to a File

  1. Attach your new label to a file.

    myfile = proj.Files(8)
    addLabel(myfile, 'Engineers', 'Sam')
    
    myfile = 
    
      ProjectFile with properties:
    
          Path: [1x72 char]
        Labels: [1x2 slproject.Label]
     ans = 
    
    Label with properties:
    
                File:  [1x86 char]
                Data: []
            DataType: 'double'
                Name: 'Sam'
        CategoryName: 'Engineers'
  2. Attach your new label to a file and assign data 2 to the label.

    addLabel(myfile, 'Engineers', 'Sam', 2)
    ans = 
    
      Label with properties:
    
                File:  [1x86 char]
                Data: 2
            DataType: 'double'
                Name: 'Sam'
        CategoryName: 'Engineers'

Query Shortcuts

  1. Examine the project's Shortcuts property.

    shortcuts = proj.Shortcuts
    shortcuts = 
    
      1x6 Shortcut array with properties:
    
        File
        RunAtStartup
        RunAtShutdown
    
  2. Examine the second shortcut in the array.

    shortcuts(2)
    ans = 
    
      Shortcut with properties:
    
                 File: 'C:\Temp\airframe\data\f14_digital_data.mat'
         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 Shortcuts node in the Simulink Project.

  3. Get the file path of the second shortcut.

    shortcuts(2).File
    ans = 
    
      C:\Temp\airframe\data\f14_digital_data.mat'
    
  4. Examine all the files in the shortcuts cell array.

    {shortcuts.File}'
    ans = 
    
        'C:\Temp\airframe\data\buses.mat'
        'C:\Temp\airframe\data\f14_digital_data.mat'
        'C:\Temp\airframe\models\slproject_f14.mdl'
        'C:\Temp\airframe\utilities\clean_up_project.m'
        'C:\Temp\airframe\utilities\rebuild_s_functions.m'
        'C:\Temp\airframe\utilities\set_up_project.m'
  5. Create a logical array that shows the shortcuts set to run at startup.

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

    startupshortcuts = shortcuts(idx)
    startupshortcuts = 
    
      1x3 Shortcut array with properties:
    
        File
        RunAtStartup
        RunAtShutdown
  7. Get the path of the third startup shortcut by accessing the File property.

    startupshortcuts(3).File
    ans =
    
    C:\Temp\airframe\utilities\set_up_project.m
    

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, shutdown scripts run.

More Project API Examples

For more examples, see:

Was this topic helpful?