Documentation

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.

sldemo_slproject_airframe
proj = simulinkproject
proj = 

  ProjectManager with properties:

             Name: 'Simulink Project Airframe Example'
           Categories: [1x1 slproject.Category]
            Shortcuts: [1x8 slproject.Shortcut]
          ProjectPath: [1x7 slproject.PathFolder]
    ProjectReferences: [1x0 slproject.ProjectReference]
                Files: [1x30 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(proj)
Methods for class slproject.ProjectManager:


addFile                       findCategory        
addFolderIncludingChildFiles  findFile            
close                         isLoaded            
createCategory                listModifiedFiles   
export                        refreshSourceControl


reload
removeCategory
removeFile 

Examine Project Files

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

  1. Examine the project files.

    files = proj.Files
    
    files = 
    
      1x30 ProjectFile array with properties:
    
        Path
        Labels
        Revision
        SourceControlStatus
    
  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.

    proj.Files(13)
    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.

    proj.Files(13).Labels
    
    ans = 
    
      Label with properties:
    
                File: 'C:\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:\slexamples\airframe\models\AnalogControl.mdl'
                     Labels: [1x1 slproject.Label]
                   Revision: '2'
        SourceControlStatus: Unmodified

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

    methods(myfile)
    
    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.

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

    modifiedfiles = listModifiedFiles(proj)
    modifiedfiles = 
    
      1x2 ProjectFile array with properties:
    
        Path
        Labels
        Revision
        SourceControlStatus

    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.

    modifiedfiles(2)
    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 = 
    
      1x29 ProjectFile array with properties:
    
        Path
        Labels
        Revision
        SourceControlStatus
    

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(category)
    
    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:\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(13);
  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'
                Data: 2
            DataType: 'double'
                Name: 'Sam'
        CategoryName: 'Assessors'
    

Query Shortcuts

  1. Examine the project's Shortcuts property.

    shortcuts = proj.Shortcuts
    shortcuts = 
    
      1x8 Shortcut array with properties:
    
        File
        RunAtStartup
        RunAtShutdown
    
  2. Examine the 8th shortcut in the array.

    shortcuts(8)
    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.

    shortcuts(5).File
    ans =
    
    C:\slexamples\airframe\reports\slproject_f14.pdf
    
    
  4. Examine all the files in the shortcuts cell array.

    {shortcuts.File}'
    ans = 
    
        'C:\slexamples\airframe\batch_jobs\checkCodeProblems.m'
        'C:\slexamples\airframe\batch_jobs\runUnitTest.m'
        'C:\slexamples\airframe\batch_jobs\saveModelFiles.m'
        'C:\slexamples\airframe\models\slproject_f14.slx'
        'C:\slexamples\airframe\reports\slproject_f14.pdf'
        'C:\slexamples\airframe\utilities\clean_up_project.m'
        'C:\slexamples\airframe\utilities\rebuild_s_functions.m'
        'C:\slexamples\airframe\utilities\set_up_project.m'
  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     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.

    startupshortcuts.File
    ans =
    
    C:\slexamples\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, the project runs shutdown scripts.

More Project API Examples

For more examples, see:

Related Examples

Was this topic helpful?