Main Content

uitree

创建树或复选框树组件

说明

t = uitree 在新图窗窗口中创建一个标准树,并返回 Tree 对象。MATLAB® 调用 uifigure 函数来创建该图窗。

t = uitree(style) 创建指定样式的树。将 style 指定为 'checkbox' 以创建复选框树,而不是标准树。

示例

t = uitree(parent) 在指定的父容器中创建标准树。父容器可以是使用 uifigure 函数创建的 Figure 或其子容器之一。

示例

t = uitree(parent,style) 在指定的父容器中创建指定样式的树。

示例

t = uitree(___,Name,Value) 使用由一个或多个 Name,Value 参数指定的属性创建一个树。可将此选项与上述语法中的任何输入参数组合一起使用。

示例

全部折叠

创建一个树,其中包含名为 Sample Data 的父节点和名为 Sample 1 的子节点。展开该树以查看这两个节点。

fig = uifigure;
t = uitree(fig);
parent = uitreenode(t,'Text','Sample Data');
child = uitreenode(parent,'Text','Sample 1');
expand(t)

Tree with two nodes. A node with text "Sample Data" has a child node with text "Sample 1".

创建一个复选框树,其中包含名为 Sample Data 的父节点和名为 Sample 1 的子节点。展开该树以查看这两个节点。

fig = uifigure;
t = uitree(fig,'checkbox');
parent = uitreenode(t,'Text','Sample Data');
child = uitreenode(parent,'Text','Sample 1');
expand(t)

Check box tree with two nodes. A node with text "Sample Data" has a child node with text "Sample 1". Both nodes have check boxes to the left of the text.

为显示文件结构的树节点添加样式,以便在视觉上区分不同文件类型。

创建一个树 UI 组件。每个顶层节点表示一个文件夹。每个子节点表示该文件夹中的一个文件。展开树以查看所有节点。

fig = uifigure("Position",[300 300 350 400]);
t = uitree(fig);

% Parent nodes
n1 = uitreenode(t,"Text","App 1");
n2 = uitreenode(t,"Text","App 2");
n3 = uitreenode(t,"Text","Images");

% Child nodes
n11 = uitreenode(n1,"Text","myapp1.m");
n21 = uitreenode(n2,"Text","myapp2.m");
n22 = uitreenode(n2,"Text","app2callback.m");
n31 = uitreenode(n3,"Text","peppers.png");

expand(t)

Tree with three top-level nodes with text "App 1", "App 2", and "Images", and nested nodes with file names.

创建三个样式:一个具有加粗字体,一个具有倾斜字体角度,一个具有图标。

dirStyle = uistyle("FontWeight","bold");
mStyle = uistyle("FontAngle","italic");
imgStyle = uistyle("Icon","peppers.png");

将粗体样式应用于顶层节点,以区分表示文件夹的节点。将斜体样式应用于 App 1App 2 节点的子节点,以区分表示 MATLAB 程序文件的节点。最后,将图标样式应用于表示一个图像文件的节点,以显示该图像的预览。

addStyle(t,dirStyle,"level",1)
addStyle(t,mStyle,"node",[n1.Children;n2.Children])
addStyle(t,imgStyle,"node",n31)

Tree UI component. The "App 1", "App 2", and "Images" nodes are bold, the nodes with file names that end in .m are italic, and the image file name has an icon of the image to its left.

创建一个按运动项目分组显示运动员姓名的 App。当 App 用户点击一个姓名时,MATLAB 会显示该运动员的相关数据。

创建一个名为 mytreeapp.m 的程序文件,并在其中包含下列代码,以创建树、一组嵌套的树节点和一个回调函数。SelectionChangedFcn 属性指定当用户点击树中的节点时要执行的函数。

function mytreeapp
    fig = uifigure;
    t = uitree(fig,"Position",[20 20 150 150]);

    % Assign callback in response to node selection
    t.SelectionChangedFcn = @nodechange;

    % First level nodes
    category1 = uitreenode(t,"Text","Runners","NodeData",[]);
    category2 = uitreenode(t,"Text","Cyclists","NodeData",[]);

    % Second level nodes.
    % Node data is age (y), height (m), weight (kg)
    p1 = uitreenode(category1,"Text","Joe","NodeData",[40 1.67 58] );
    p2 = uitreenode(category1,"Text","Linda","NodeData",[49 1.83 90]);
    p3 = uitreenode(category2,"Text","Rajeev","NodeData",[25 1.47 53]);
    p4 = uitreenode(category2,"Text","Anne","NodeData",[88 1.92 100]);

    % Expand the tree
    expand(t);
    
    % Create the function for the SelectionChangedFcn callback
    % When the function is executed, it displays the data of the selected item
    function nodechange(src,event)
        node = event.SelectedNodes;
        display(node.NodeData);
    end
end

当用户运行 mytreeapp 并点击树中的节点时,MATLAB 将显示该节点的 NodeData

Tree UI component with parent nodes labeled "Runners" and "Cyclists". Each parent node has two child nodes with athlete names.

创建一个按食品类别分组显示杂货列表的 App。App 用户可以选中单个项或整个食品类别,MATLAB 会显示选中项的总重量。

创建一个名为 mycheckboxtreeapp.m 的程序文件,并在其中包含下列命令,以创建一个复选框树、一组嵌套的树节点和两个用于复选框树的回调函数。CheckedNodesChangedFcn 属性指定当用户选中或取消选中树中的节点时要执行的函数。SelectedNodesChangedFcn 属性指定当用户选择树中的节点时要执行的函数。

function mycheckboxtreeapp
    fig = uifigure;
    cbt = uitree(fig,'checkbox','Position',[20 20 150 150]);
    
    % Assign callbacks in response to node check and selection
    cbt.CheckedNodesChangedFcn = @checkchange;
    cbt.SelectionChangedFcn = @selectchange;
    
    % First level nodes
    category1 = uitreenode(cbt,'Text','Vegetables','NodeData',[]);
    category2 = uitreenode(cbt,'Text','Fruits','NodeData',[]);

    % Second level nodes.
    % Node data is the weight of the food item (in grams)
    p1 = uitreenode(category1,'Text','Cucumber','NodeData',400);
    p2 = uitreenode(category1,'Text','Carrot','NodeData',65);
    p3 = uitreenode(category2,'Text','Apple','NodeData',183);
    p4 = uitreenode(category2,'Text','Banana','NodeData',120);

    % Expand the tree
    expand(cbt);
    
    % Create the function for the CheckedNodesChangedFcn callback
    % When this function is executed, it displays the total weight
    % of all checked items
    function checkchange(src,event)
        nodes = event.LeafCheckedNodes;
        if ~isempty(nodes)
            data = [nodes.NodeData];
            display(sum(data));
        end
    end

    % Create the function for the SelectedNodesChangedFcn callback
    % When this function is executed, it displays the name
    % of the selected item
    function selectchange(src,event)
        node = event.SelectedNodes;
        display(node.Text);
    end
end

当用户运行 mycheckboxtreeapp 并选中或取消选中树中的节点时,MATLAB 会显示所有二级选中节点的重量(存储在 NodeData 中)总和。当用户选择树中的一个节点时,MATLAB 会显示该节点的文本。

Check box tree with two top-level nodes, "Vegetables" and "Fruits". Each top-level node has two nested nodes beneath it.

创建一个基于表中数据填充节点的树。

使用网格布局管理器创建一个图窗来容纳 UI 组件。加载电力公司停电的采样数据,并创建一个表 UI 组件来显示该数据。然后,创建一个树来容纳列出断电区域和原因的节点。

fig = uifigure;
gl = uigridlayout(fig,[1 2]);
gl.ColumnWidth = {'2x','1x'};

T = readtable("outages.csv");
T = T(1:20,["Region","OutageTime","Loss","Cause"]);
tbl = uitable(gl,"Data",T);

tr = uitree(gl);

指定要在树中显示的表变量。对于其中每一个变量,创建一个顶层节点,其文本就是变量名称。通过将变量的表条目转换为分类数组并将分类列表返回为 names,提取相关数据。然后,遍历各类别。对于每个元素,向树中适当的父节点下添加一个节点。

vars = ["Region","Cause"];

for k1 = 1:length(vars)
    var = vars{k1};
    varnode = uitreenode(tr,"Text",var);
    rows = T{:,var};
    names = categories(categorical(rows));
         
    for k2 = 1:length(names)
        text = names{k2};
        uitreenode(varnode,"Text",text);
    end
end

展开树以查看所有节点。

expand(tr)

Figure window with a table and a tree. The table contains outage sample data, and the tree contains a node for each region and cause in the table data.

输入参数

全部折叠

树的样式,指定为以下项之一:

  • 'tree' - 项的分层列表

  • 'checkbox' - 可选中的项的分层列表,每个项的左侧都有一个复选框

父容器,指定为使用 uifigure 函数创建的 Figure 对象或其子容器之一:TabPanelButtonGroupGridLayout。如果不指定父容器,MATLAB 会调用 uifigure 函数创建新 Figure 对象充当父容器。

名称-值参数

指定可选的、以逗号分隔的 Name,Value 对组参数。Name 为参数名称,Value 为对应的值。Name 必须放在单引号 (' ') 中。您可以指定多个名称-值对组参数,如 Name1,Value1,...,NameN,ValueN

每种类型的 Tree 对象支持一组不同的属性。有关每种类型的属性和描述的完整列表,请参阅相关联的属性页。

详细信息

全部折叠

选定的节点

在标准树或复选框树中,选定的节点通过蓝色高亮显示节点文本来表示。App 用户可以通过点击节点文本来选择节点。

Multiselect 属性设置为 'off' 的标准树和每个复选框树中,任何时候都最多只能选定一个节点。在标准树中,您可以将 Multiselect 属性设置为 'on' 以允许选择多个节点。

在此图像中,选择了 Carrot 节点。

Check box tree. The node with text "Carrot" has a blue highlight.

选中的节点

在复选框树中,选中的节点由节点文本左侧的选中复选框表示。可以选中任意数量的节点。App 用户可以通过点击复选框来选中或取消选中某个节点。在标准树中,无法通过复选框方式选择节点。

在此图像中,选中了 FruitsAppleBanana 节点。

Check box tree. The nodes with text "Fruit", "Apple", and "Banana" have checked check boxes to the left of the text.

版本历史记录

在 R2017b 中推出

全部展开