Main Content

findLabel

Get project file label

Description

label = findLabel(file,categoryName,labelName) gets the specified label labelName, in the specified category categoryName, from the specified file file. The function returns the label definition and the attached data. Use this syntax when you know the label name and category. If the label is not found, findLabel returns an empty array.

example

label = findLabel(file,labelDefinition) gets the label defined by the specified label definition object. Use this syntax if you previously created a labelDefinition using a Labels property, for instance by using an expression like myfile.Labels(1).

example

label = findLabel(category,labelName) gets a label using a category object rather than a file name and category name. Use this syntax if you created a category object using the proj.Categories property or the findCategory function.

example

Examples

collapse all

Find all project files with the label "Utility".

Open the Times Table App project. Use currentProject to create a project object from the currently loaded project.

openExample("matlab/TimesTableProjectExample")
proj = currentProject;

Get the list of project files.

files = proj.Files;

Loop through the files. Get each file's extension by taking the last element returned by the fileparts function. If the file has the extension .m, attach the label "Utility".

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

Use the findLabel function to find all the files with the label "Utility".

utilityFilesToReview = [findLabel(files,"Classification","Utility").File]';

Open the Times Table App project. Use currentProject to create a project object from the currently loaded project.

openExample("matlab/TimesTableProjectExample")
proj = currentProject;

Get a file by name.

myfile = findFile(proj,"source/timesTableGame.m");

Get a label from that file by name.

label = findLabel(myfile,"Classification","Design");
label = 

  Label with properties:

            File: "C:\myProjects\examples\TimesTableApp\source\timesTableGame.m"
        DataType: 'none'
            Data: []
            Name: "Design"
    CategoryName: "Classification"

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

labels = myfile.Labels
labeldefinition = myfile.Labels(1)

Get a label from the label definition.

label = findLabel(myfile,labeldefinition);

Open the Times Table App project. Use currentProject to create a project object from the currently loaded project.

openExample("matlab/TimesTableProjectExample")
proj = currentProject;

Get a category.

category = proj.Categories(1)
category = 

  Category with properties:

                Name: "Classification"
        SingleValued: 1
            DataType: "none"
    LabelDefinitions: [1×7 matlab.project.LabelDefinition]

Get a label definition from that category.

ld = findLabel(category,"Design")
ld = 

  LabelDefinition with properties:

            Name: "Design"
    CategoryName: "Classification"

Input Arguments

collapse all

File to search in, specified as a ProjectFile object or an array of ProjectFile objects. You can create a project file object by examining the project files property (using the syntax proj.Files), or use findFile to get a file by name. The file must be in the specified project.

Name of category for the label, specified as a character vector or string scalar.

Name of label, specified as a character vector or string scalar.

Label definition, specified as a LabelDefinition object gotten from the file.Label property.

Category object. Create a category object from the proj.Categories property or by using the findCategory function.

Output Arguments

collapse all

Label, returned as a Label object.

Version History

Introduced in R2019a