Main Content

uitree

Create standard tree or check box tree component

Description

t = uitree creates a standard tree in a new figure window and returns the Tree object. MATLAB® calls the uifigure function to create the figure.

t = uitree(style) creates a tree of the specified style. Specify style as 'checkbox' to create a check box tree instead of a standard one.

example

t = uitree(parent) creates a standard tree in the specified parent container. The parent can be a Figure created using the uifigure function, or one of its child containers.

example

t = uitree(parent,style) creates a tree of the specified style in the specified parent container.

example

t = uitree(___,Name,Value) creates a tree with properties specified by one or more Name,Value pair arguments. Use this option with any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Create a tree that contains one node called Sample Data.

fig = uifigure;
t = uitree(fig);
node = uitreenode(t,'Text','Sample Data');

Create a check box tree that contains a parent node called Sample Data with a child node called Sample 1. Expand the tree to see both nodes.

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

Create an app that displays athlete names grouped by sport. When the app user clicks on a name, MATLAB displays data about the athlete.

Create a program file called mytreeapp.m that contains the following commands to create a tree, a set of nested tree nodes, and a callback function for the tree. The SelectionChangedFcn property specifies the function to execute when the user clicks a node in the tree.

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

When the user runs mytreeapp and clicks a node in the tree, MATLAB displays the NodeData for that node.

Create an app that displays a grocery list grouped by food category. The app user can check individual items or entire food categories, and MATLAB displays the total weight of the checked items.

Create a program file called mycheckboxtreeapp.m that contains the following commands to create a check box tree, a set of nested tree nodes, and two callback functions for the check box tree. The CheckedNodesChangedFcn property specifies the function to execute when the user checks or unchecks a node in the tree. The SelectedNodesChangedFcn property specifies the function to execute when the user selects a node in the tree.

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

When the user runs mycheckboxtreeapp and checks or unchecks a node in the tree, MATLAB displays the sum of the weights (stored in NodeData) for all the second-level checked nodes. When the user selects a node in the tree, MATLAB displays the text of that node.

Input Arguments

collapse all

Style of tree, specified as one of the following:

  • 'tree' — Hierarchical list of items

  • 'checkbox' — Hierarchical list of items that can be checked, presented with a check box to the left of each item

Parent container, specified as a Figure object created using the uifigure function, or one of its child containers: Tab, Panel, ButtonGroup, or GridLayout. If you do not specify a parent container, MATLAB calls the uifigure function to create a new Figure object that serves as the parent container.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments as Name1,Value1,...,NameN,ValueN.

Each type of Tree object supports a different set of properties. For a full list of properties and descriptions for each type, see the associated property page.

More About

collapse all

Selected Nodes

In a standard tree or a check box tree, a selected node is indicated by a blue highlight around the node text. The app user can select a node by clicking on the node text.

In a standard tree with the Multiselect property set to 'off' and in every check box tree, at most one node can be selected at any time. In a standard tree, you can set the Multiselect property to 'on' to allow for multiple nodes to be selected.

In the following image, the Carrot node is selected.

Checked Nodes

In a check box tree, a checked node is indicated by a checked check box to the left of the node text. Any number of nodes can be checked. The app user can check or uncheck a node by clicking on the check box. In a standard tree, you cannot check nodes.

In the following image, the Fruits, Apple, and Banana nodes are checked.

Introduced in R2017b