In my previous posts, I introduced the semi-documented uitree function that enables displaying data in a hierarchical (tree) control in Matlab GUI, and showed how it can be customized. Today, I will continue by describing how specific uitree nodes can be customized.
To start the discussion, let’s re-create last week’s simple uitree:
% Fruits fruits = uitreenode('v0', 'Fruits', 'Fruits', [], false); fruits.add(uitreenode('v0', 'Apple', 'Apple', [], true)); fruits.add(uitreenode('v0', 'Pear', 'Pear', [], true)); fruits.add(uitreenode('v0', 'Banana', 'Banana', [], true)); fruits.add(uitreenode('v0', 'Orange', 'Orange', [], true)); % Vegetables veggies = uitreenode('v0', 'Veggies', 'Vegetables', [], false); veggies.add(uitreenode('v0', 'Potato', 'Potato', [], true)); veggies.add(uitreenode('v0', 'Tomato', 'Tomato', [], true)); veggies.add(uitreenode('v0', 'Carrot', 'Carrot', [], true)); % Root node root = uitreenode('v0', 'Food', 'Food', [], false); root.add(veggies); root.add(fruits); % Tree figure('pos',[300,300,150,150]); mtree = uitree('v0', 'Root', root);

User-created tree
Labels
Node labels (descriptions) can be set using their Name property (the second uitreenode data argument). Note that the horizontal space allotted for displaying the node name will not change until the node is collapsed or expanded. So, if the new name requires more than the existing space, it will be displayed as something like “abc…”, until the node is expanded or collapsed.
Node names share the same HTML support feature as all Java Swing labels. Therefore, we can specify font size/face/color, bold, italic, underline, super-/sub-script etc.:
txt1 = '<html><b><u><i>abra</i></u>'; txt2 = '<font color="red"><sup>kadabra</html>'; node.setName([txt1,txt2]);

HTML-enriched tree nodes
Icons
Tree-node icons can be specified during node creation, as the third data argument to uitreenode, which accepts an icon-path (a string):
iconPath = fullfile(matlabroot,'/toolbox/matlab/icons/greenarrowicon.gif'); node = uitreenode('v0',value,name,iconPath,isLeaf);
Tree node icons can also be created or modified programmatically in run-time, using Matlab’s im2java function. Icons can also be loaded from existing files as follows (real-life programs should check and possibly update jImage’s size to 16 pixels, before setting the node icon, otherwise the icon might get badly cropped; also note the tree-refreshing action):
jImage = java.awt.Toolkit.getDefaultToolkit.createImage(iconPath); veggies.setIcon(jImage); veggies.setIcon(im2java(imread(iconPath))); % an alternative % refresh the veggies node (and all its children) mtree.reloadNode(veggies);

Setting node icon
Behavior
Nodes can be modified from leaf (non-expandable) to parent behavior (=expandable) by setting their LeafNode property (a related property is AllowsChildren):
set(node,'LeafNode',false); % =expandable node.setLeafNode(0); % an alternative
One of the questions I was asked was how to “disable” a specific tree node. One way would be to modify the tree’s ExpandFcn callback. Another way is to use a combination of HTML rendering and the node’s AllowsChildren property:
label = char(veggies.getName); veggies.setName(['<html><font color="gray">' label]); veggies.setAllowsChildren(false); t.reloadNode(veggies);

Disabled node
Another possible behavioral customization is adding a context-menu to a uitree. We can set node-specific tooltips using similar means.
Answering a reader’s request from last week, tree nodes icons can be used to present checkboxes, radio buttons and other similar node-specific controls. This can actually be done in several ways, that will be explored in next week’s article.
There are numerous other possible customizations – if readers are interested, perhaps I will describe some of them in future articles. If you have any special request, please post a comment below.
Related posts:
- Customizing uitree nodes – part 2 This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...
- Customizing uitree This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...
- uitree This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...
- An interesting uitree utility ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...
- Adding a context-menu to a uitree uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....
- JMI wrapper – local MatlabControl part 1 MatlabControl is an open-source wrapper of JMI that allows an easy and documented way to communicate from Java to Matlab. This article describes this wrapper....


How can you get the path from the node if you are using uitree for a file system? Also very interested in using check boxes on the nodes.
@Amy – here’s how to get the file-system path from your tree:
Re checkbox nodes – look at tomorrow’s article…
Simply excellent…. This is what I am looking for long. Thanks a bunch.
Hello Yair,
Thank you very much for all your usefull explanations.
I have to implement node-specific tooltips. As you mention, I try to follow the uitree context menu example, and also look at tree-node’s renderer, but without success.
So I would be very insterested in getting some examples.
Thanks a lot!