Main Content

addLabel

将标签附加到工程文件

说明

示例

addLabel(file,categoryName,labelName) 将指定类别中的指定标签附加到指定文件。

示例

addLabel(file,categoryName,labelName,labelData) 附加具有指定的文本或数值数据的标签。您无法将标签数据添加到内置标签,因为它们为只读。

示例

全部折叠

打开 Times Table App 工程。使用 currentProject 从当前加载的工程创建一个工程对象。

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

按名称获取一个文件。

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

  ProjectFile with properties:

                   Path: "C:\myProjects\examples\TimesTableApp\source\timesTableGame.m"
                 Labels: [1×1 matlab.project.Label]
               Revision: "286043ae7ee557100902fb645a6c97eca5d50472"
    SourceControlStatus: Unmodified

通过获取文件的 Labels 属性查看现有标签。

myfile.Labels
ans = 

  Label with properties:

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

将标签 "Artifact" 附加到 "Classification" 类别的文件上。

addLabel(myfile,"Classification","Artifact")
ans = 

  Label with properties:

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

现在有两个标签,即原始标签和添加的标签。要仅查看添加的标签,请对 Labels 属性进行索引。

reviewlabel = myfile.Labels(1)
reviewlabel = 

  Label with properties:

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

撤除文件的新标签。文件现在只有一个标签。

removeLabel(myfile,reviewlabel)
myfile
myfile = 

  ProjectFile with properties:

                   Path: "C:\myProjects\examples\TimesTableApp\source\timesTableGame.m"
                 Labels: [1×0 matlab.project.Label]
               Revision: "286043ae7ee557100902fb645a6c97eca5d50472"
    SourceControlStatus: Unmodified

"Classification" 类别中的标签 "Utility" 附加到工程中所有扩展名为 .m 的文件。

打开 Times Table App 工程。使用 currentProject 从当前加载的工程创建一个工程对象。

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

获取文件列表。

files = proj.Files;

遍历每个文件。为仅获取文件扩展名,可使用 fileparts 函数来提取最后一部分。如果文件扩展名为 .m,则附加标签 "Utility"

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

在工程的文件视图中,对于 utilities 文件夹中每个 .m 文件,分类列会显示标签 Utility

创建标签类别 "Review" 和标签 "To Review",然后将标签和标签数据附加到一个文件。您无法将标签数据添加到内置标签,因为它们为只读。

打开 Times Table App 工程。使用 currentProject 从当前加载的工程创建一个工程对象。

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

创建一个新类别 "Review"

createCategory(proj,"Review","char");

对于该新类别,创建标签 "To Review"

reviewCategory = findCategory(proj,"Review");
createLabel(reviewCategory,"To Review");

按名称获取一个文件。

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

  ProjectFile with properties:

                   Path: "C:\myProjects\examples\TimesTableApp\source\timesTableGame.m"
                 Labels: [1×1 matlab.project.Label]
               Revision: "c7603d5dd71a40484974c3f1c45d839819b7b061"
    SourceControlStatus: Unmodified

将标签 "To Review" 和标签数据字符向量附加到该文件。

addLabel(myfile,"Review","To Review","Whole team design review")
ans = 

  Label with properties:

            File: "C:\myProjects\examples\TimesTableApp\source\timesTableGame.m"
        DataType: 'char'
            Data: 'Whole team design review'
            Name: "To Review"
    CategoryName: "Review"

在工程的文件视图中,对于 timesTableGame.m 文件,Review 列显示具有标签数据的 To Review 标签。

您也可以使用 Data 属性设置或更改标签数据。

mylabel = myfile.Labels(1);
mylabel.Data = "Final review";

输入参数

全部折叠

要添加标签的文件,指定为 ProjectFile 对象。您可以通过检查工程的文件属性 (proj.Files) 来获取 ProjectFile 对象,或通过使用 findFile 按名称查找文件。文件必须在工程中。

标签类别的名称,指定为字符向量或字符串标量。

要附加的标签的名称,指定为字符向量、字符串标量,或指定为 file.Label 属性或 findLabel 函数返回的 LabelDefinition 对象。您可以指定工程中尚不存在的新标签名称。

要附加到标签的数据,指定为字符向量、字符串标量或数值。数据类型取决于标签定义。使用 file.LabelfindLabel 获得标签以检查其 DataType 属性。

版本历史记录

在 R2019a 中推出