Main Content

Label Management in MATLAB and Simulink Projects

This example shows how to programmatically manage labels in a project.

Examine File Labels

Create a project object.

proj = currentProject;

Examine the files in the project.

files = proj.Files
files=1×30 ProjectFile array with properties:
    Path
    Revision
    SourceControlStatus
    Labels

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

aFile = files(2);

Find information about the attached labels by indexing into the Labels property of the file object.

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

            File: "/tmp/Bdoc24a_2528353_1894899/tpd3b7eb73/simulink-ex81014015/airframe/custom_tasks/analyzeModelFiles.m"
        DataType: "none"
            Data: []
            Name: "Analysis"
    CategoryName: "Classification"

Attach Label to Subset of Files

Attach the Design label in the Classification category to all the files with the .m extension.

files = proj.Files;
for fileIdx = 1:numel(files)
   file = files(fileIdx); 
   [~, ~, fileExtension] = fileparts(file.Path);
   if strcmp(fileExtension,".m")
       addLabel(file,"Classification","Design");
   end
end

Find Named Label

Create a project file object for the rebuild_s_functions file by using the findFile function.

pathToLocate = fullfile("utilities","rebuild_s_functions.m");
file = findFile(proj,pathToLocate);

Define an array of label objects, one for each label attached to the file, by using the Labels property.

labels = file.Labels
labels = 
  Label with properties:

            File: "/tmp/Bdoc24a_2528353_1894899/tpd3b7eb73/simulink-ex81014015/airframe/utilities/rebuild_s_functions.m"
        DataType: "none"
            Data: []
            Name: "Design"
    CategoryName: "Classification"

Find the label by using the findLabel function on the project file object.

label = findLabel(file,"Classification","Design")
label = 
  Label with properties:

            File: "/tmp/Bdoc24a_2528353_1894899/tpd3b7eb73/simulink-ex81014015/airframe/utilities/rebuild_s_functions.m"
        DataType: "none"
            Data: []
            Name: "Design"
    CategoryName: "Classification"

Create New Category and Labels

Create a category of labels called Engineers to indicate the owner of a file by using the createCategory function.

createCategory(proj,"Engineers","char");
engineersCategory = findCategory(proj,"Engineers");

Use the createLabel function to create different labels.

createLabel(engineersCategory,"Sam");
createLabel(engineersCategory,"Pat");
createLabel(engineersCategory,"Alex");

Assign ownership of the rebuild_s_functions file to Sam by using the Sam label from the Engineers category. For more information, see addLabel and findLabel.

addLabel(file,"Engineers","Sam");
label = findLabel(file,"Engineers","Sam");

Set Label Data

Set the data for the attached label.

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

            File: "/tmp/Bdoc24a_2528353_1894899/tpd3b7eb73/simulink-ex81014015/airframe/utilities/rebuild_s_functions.m"
        DataType: "char"
            Data: 'Maintenance responsibility'
            Name: "Sam"
    CategoryName: "Engineers"

Related Topics

Add Labels to Project Files

Manage Project Files