uitree

Create tree component

Description

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

example

t = uitree(Name,Value) specifies Tree property values using one or more Name,Value pair arguments.

t = uitree(parent) creates a 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,Name,Value) creates the tree in the specified container and sets one or more Tree property values.

Examples

collapse all

Create a tree that contains one node called Sample Data.

t = uitree('Position',[20 20 150 150]);
node = uitreenode(t,'Text','Sample Data');

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 Tree 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);
    
    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.

Input Arguments

collapse all

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

Example: t = uitree('Position',[100 100 150 150]) creates a tree with a specific location and size.

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.

The properties listed here are a subset. For a full list, see Tree Properties.

Selected nodes, specified as a TreeNode object or an array of TreeNode objects. Use this property to get or set the selected nodes in a tree.

To allow users to select multiple nodes, set the Multiselect property to 'on'. MATLAB always returns SelectedNodes as a column vector when the tree has multiple selected nodes.

Multiple node selection, specified as 'off' or 'on', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

Set this property to 'on' to allow users to select multiple nodes simultaneously.

Selection changed callback, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

Use this callback function to execute commands when the user selects a different node in the tree.

This callback function can access specific information about the user’s interaction with the tree, such as the selected nodes. MATLAB passes this information in a SelectedNodesChangedData object as the second argument to your callback function. In App Designer, the argument is called event. You can query the object properties using dot notation. For example, event.SelectedNodes returns the selected TreeNode object or objects. The SelectedNodesChangedData object is not available to callback functions specified as character vectors.

The following table describes properties of the SelectedNodesChangedData object.

Property

Description

SelectedNodes

Most recently selected TreeNode object or objects

PreviousSelectedNodes

Previously selected TreeNode object or objects

Source

Component that executes the callback

EventName

'SelectionChanged'

For more information about writing callbacks, see Write Callbacks in App Designer.

Location and size, specified as a four-element vector of the form [left bottom width height]. This table describes each element in the vector. All measurements are in pixel units.

ElementDescription
leftDistance from the inner left edge of the parent container to the left edge of the bounding box that encloses the tree
bottomDistance from the inner bottom edge of the parent container to the bottom edge of the bounding box that encloses the tree
widthDistance between the right and left edges of the bounding box
heightDistance between the top and bottom edges of the bounding box

Introduced in R2017b