Documentation Center

  • Trial Software
  • Product Updates

Automate Label Management in a Simulink® Project

This example shows how to the Simulink® Project functions to manage labels.

Open the Airframe Example Project

Run the following commands to create and open a working copy of the "sldemo_slproject_airframe" example.

Simulink.ModelManagement.Project.projectDemo('airframe');
rebuild_s_functions;
Creating sandbox for project.
Adding files to source control.
Created temporary Simulink Project example files in "/tmp/R2014ad_357_3462/20140117T184744/airframe"
Building with 'gcc'.
MEX completed successfully.

Get a Project Object.

Use the simulinkproject function to get a project object to manipulate the currently open Simulink Project at the command line.

project = simulinkproject;

View the Labels of a File

Examine the Files property of the project. The Files property contains an array of file objects, one for each file in the project.

files = project.Files;
disp(files);
  1x27 ProjectFile array with properties:

    Path
    Labels

You can use indexing to access files in this array. The following command gets file number 2. Each file has two properties describing its path and attached labels.

aFile = files(2);
disp(aFile);
  ProjectFile with properties:

      Path: '/tmp/R2014ad_357_3462/20140117T184744/airframe/batch_jobs/che...'
    Labels: [1x1 slproject.Label]

Find information about a file's attached labels by indexing into the file object's Labels property. The following command gets the first label attached to this particular file.

label = aFile.Labels(1);
disp(label);
  Label with properties:

            File: '/tmp/R2014ad_357_3462/20140117T184744/airframe/batch_jo...'
            Data: []
        DataType: 'none'
            Name: 'Utility'
    CategoryName: 'Classification'

Attach a Label to a Subset of Files.

The following code attaches the label 'To Review' in the 'Review' category to all files in the project with the .m file extension.

First get the list of files:

files = project.Files;

Then loop through each file and attach the label 'To Review' from the 'Review' category if the file has the extension .m.

for fileIdx = 1:numel(files)
   file = files(fileIdx);
   [~, ~, fileExtension] = fileparts(file.Path);
   if strcmp(fileExtension,'.m')
       addLabel(file, 'Review', 'To Review');
   end
end

Find a Named Label

You can set and query data on a label that is attached to a file. To do this, you first need to find the file object. You can do this by looping through all files in the project, as shown in the previous step. Alternatively, you can use the findFile function on the project.

The following code finds the file object for the file 'utilities/set_up_project.m'.

pathToLocate = fullfile('utilities','clean_up_project.m');
file = findFile(project, pathToLocate);

Examine the Labels property to get an array of Label objects, one for each label attached to the file.

labels = file.Labels;
disp(labels);
  1x2 Label array with properties:

    File
    Data
    DataType
    Name
    CategoryName

To find a label by name, use findLabel on the file object.

label = findLabel(file, 'Review','To Review');

Set Label Data

The following command sets the data for this label.

label.Data = 'Please review this file';

Create a New Category

You must create new labels before you can attach them to a file. You define labels in categories, giving each category a name and supported data type.

The following code creates a category of labels called Engineers which can be used to denote file ownership in a project. These labels have the char datatype for attaching String data.

createCategory(project,'Engineers','char');
engineersCategory = findCategory(project, 'Engineers');
createLabel(engineersCategory,'Sam');
createLabel(engineersCategory,'Pat');
createLabel(engineersCategory,'Alex');

You can now attach an Sam label from the 'Engineers' category to a file in the project.

addLabel(file, 'Engineers', 'Sam');
label = findLabel(file, 'Engineers', 'Sam');
label.Data = 'Maintainance responsibility';
disp(label)
  Label with properties:

            File: '/tmp/R2014ad_357_3462/20140117T184744/airframe/utilitie...'
            Data: 'Maintainance responsibility'
        DataType: 'char'
            Name: 'Sam'
    CategoryName: 'Engineers'

Further Information

Simulink Project documentationSimulink Project documentation

Was this topic helpful?