Main Content

Automate Label Management in a Project

This example shows how to use the project functions to manage labels.

Open the Airframe Example Project

Create and open a working copy of the project example files. MATLAB® copies the files to an example folder so that you can edit them.

sldemo_slproject_airframe;
Starting: Simulink
Building with 'MinGW64 Compiler (C)'.
MEX completed successfully.

Get a Project Object.

Create a project object to manipulate the currently open project at the command line.

project = currentProject;

View the Labels of a File

Examine the files in the project.

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

    Path
    Labels
    Revision
    SourceControlStatus

Use indexing to access files in this list. For example, get file number 10. Each file has two properties describing its path and attached labels.

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

                   Path: "C:\workSpace\examples\airframe2\data\system_model.sldd"
                 Labels: [1×1 matlab.project.Label]
               Revision: "57973f5e87ca65215ab688e3544287e07db794c4"
    SourceControlStatus: Unmodified

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

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

            File: "C:\workSpace\examples\airframe2\data\system_model.sldd"
        DataType: 'none'
            Data: []
            Name: "Design"
    CategoryName: "Classification"

Attach a Label to a Subset of Files.

The following code attaches the label Design in the Classification 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 Design from the Classification 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, 'Classification', 'Design');
   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/rebuild_s_functions.m'.

pathToLocate = fullfile('utilities','rebuild_s_functions.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);
  Label with properties:

            File: "C:\workSpace\examples\airframe2\utilities\rebuild_s_functions.m"
        DataType: 'none'
            Data: []
            Name: "Design"
    CategoryName: "Classification"

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

label = findLabel(file, 'Classification','Design');
disp(label);
  Label with properties:

            File: "C:\workSpace\examples\airframe2\utilities\rebuild_s_functions.m"
        DataType: 'none'
            Data: []
            Name: "Design"
    CategoryName: "Classification"

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');

Set Label Data

The following command sets the data for the attached label.

label.Data = 'Maintenance responsibility';
disp(label)
  Label with properties:

            File: "C:\workSpace\examples\airframe2\utilities\rebuild_s_functions.m"
        DataType: 'char'
            Data: 'Maintenance responsibility'
            Name: "Sam"
    CategoryName: "Engineers"

Further Information

Project Management