Main Content


Collapse tree node



collapse(parent) collapses the nodes of the parent tree or tree node. If parent is a Tree object, then the top-level nodes in the tree display in a collapsed state. Any children beyond the top level do not collapse. If parent is a TreeNode object, then the specified node displays in a collapsed state, but the children inside that node do not collapse.

collapse(parent,'all') collapses all child nodes of the parent tree or tree node.


collapse all

Create a tree that has three levels of nodes, and expand all of the nodes.

fig = uifigure;
tree = uitree(fig,'Position',[20 20 175 100]);
% First level nodes
category1 = uitreenode(tree,'Text','Runners');
category2 = uitreenode(tree,'Text','Cyclists');

% Second level nodes
equip1 = uitreenode(category1,'Text','Equipment');
equip2 = uitreenode(category2,'Text','Equipment');
% Third level nodes
shoes = uitreenode(equip1,'Text','Running Shoes');
bike = uitreenode(equip2,'Text','Bicycle');
helmet = uitreenode(equip2,'Text','Helmet');

% Expand all nodes

Tree UI component with three levels of nodes. All nodes with children are expanded.

Collapse the top-level nodes.


Tree UI component with two collapsed nodes

Expand the Cyclists node.


Tree UI component with two top-level nodes. The first node is collapsed, and the second node and its child node are expanded.

The Equipment child node is already expanded because the previous collapse command collapsed only the Cyclists node. If you want to collapse all the child nodes, specify the 'all' option when you call the collapse function.

Input Arguments

collapse all

Parent tree or node, specified as a Tree object or TreeNode object.

Version History

Introduced in R2017b