findLabel

Get project file label

Description

example

label = findLabel(file,categoryName,labelName) gets the specified label, in the specified category, from the specified file. It returns the label definition and its 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 got a labelDefinition by accessing 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 have a category object gotten from the proj.Categories property or by using the findCategory function.

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.

matlab.project.example.timesTable
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" and add them to the array utilityFilesToReview.

utilityFilesToReview = {};
for jj=1:numel(files)
   thisFile = files(jj);   
   label = findLabel(thisFile,"Classification","Utility");
   if (~isempty(label))
      % This is a file labeled "Utility". Add to the 
      % list of utility files.
      utilityFilesToReview = [utilityFilesToReview; thisFile];
   end
end

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

matlab.project.example.timesTable
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.

matlab.project.example.timesTable
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 file objects. You can get the file object by examining the project’s 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. Get a category object from the proj.Categories property or by using the findCategory function.

Output Arguments

collapse all

Label, returned as a Label object.

Introduced in R2019a