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

proj = simulinkproject
proj = 

  ProjectManager with properties:

             Name: 'Simulink Project Airframe Example'
       Categories: [1x1 slproject.Category]
        Shortcuts: [1x10 slproject.Shortcut]
      ProjectPath: [1x6 slproject.PathFolder]
ProjectReferences: [1x1 slproject.ProjectReference]
            Files: [1x26 slproject.ProjectFile]
       RootFolder: 'C:\Work\temp\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   export   reload
findCategory  removeCategory
close  findFile  removeFile
createCategory  isLoaded

Examine Project Files

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

  1. Examine the project files.

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

    ans = 
      ProjectFile with properties:
          Path: 'C:\Work\temp\slexamples\airframe\models\AnalogControl.mdl'
        Labels: [1x1 slproject.Label]
  3. Examine the labels of the ninth file.

    ans = 
      Label with properties:
                File: 'C:\Work\temp\slexamples\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: 'C:\Work\temp\slexamples\airframe\models\AnalogControl.mdl'
        Labels: [1x1 slproject.Label]

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

    Methods for class slproject.ProjectFile:
    addLabel     findLabel    removeLabel

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'
                DataType: 'char'
        LabelDefinitions: []

    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 string Please assess.

    label = findLabel(myfile, 'Engineers', 'Bob');
    label.Data = 'Please assess'
    label = 
      Label with properties:
                File: 'C:\Work\temp\slexamples\airframe\models\AnalogControl.mdl'
                Data: 'Please assess'
            DataType: 'char'
                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(9);
  4. Attach label to file and assign data value 2 to the label.

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

Query Shortcuts

  1. Examine the project's Shortcuts property.

    shortcuts = proj.Shortcuts
    shortcuts = 
      1x10 Shortcut array with properties:
  2. Examine the fifth shortcut in the array.

    ans = 
      Shortcut with properties:
                 File: 'C:\Work\temp\slexamples\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 Shortcut Management node in the Simulink Project.

  3. Get the file path of the second 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     1     1     0     0     0     0     1
  6. Use the logical array to get only the startup shortcuts.

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

    ans =

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 software runs shutdown scripts.

More Project API Examples

For more examples, see:

Was this topic helpful?