Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

uitree

(App Designer) Create tree component

Use only with App Designer or figures created with the uifigure function. GUIDE and figures created with the figure function do not support trees.

Syntax

t = uitree
t = uitree(Name,Value)
t = uitree(parent)
t = uitree(parent,Name,Value)

Description

t = uitree creates a tree UI component in a new figure 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 container can be a Figure created using the uifigure function, or one of its child containers: Tab, Panel, or ButtonGroup.

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
    f = uifigure;
    t = uitree(f,'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 object, specified as a Figure object created using the uifigure function, or one of its child containers: Tab, Panel, or ButtonGroup.

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.

collapse all

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'. 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. For example, 'disp(''hello world'')' calls the disp function. 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'

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

Output Arguments

collapse all

Tree object. Use t to set properties of the tree after creating it.

Introduced in R2017b

Was this topic helpful?