- Undocumented Matlab - https://undocumentedmatlab.com -

An interesting uitree utility

Posted By Yair Altman On March 30, 2011 | 5 Comments

“uitree” is consistently one of the most-popular search terms on this site, together with “uitable” and “uitab”. I also receive frequent questions about tree components in Matlab.
I already wrote a series of articles on Matlab’s built-in semi-documented [1] uitree (starting here [2]), and I am on a constant lookout for additional interesting angles on uitree to post here. Brett Shoelson’s recent review [3] of 2005’s Pick-of-the-Week utilities, provided me with such an opportunity.
One of 2005’s featured utilities, Structure Explorer [4] by Hassan Lahdili, originally written using Microsoft’s TreeView ActiveX control, was apparently rewritten using Matlab’s Java-based uitree. This enables the utility to run on non-Windows platforms and have a more consistent look-and-feel with Matlab’s Java-based GUI:

Hassan Lahdili's Structure Explorer (click to enlarge) [5]
Hassan Lahdili's Structure Explorer (click to enlarge)

Hassan’s ExploreStruct utility, in addition to being useful as-is, provides a very useful learning tool for uitree integration in Matlab GUI. This utility uses custom icons for different node types, custom callbacks for node expansion (NodeWillExpandCallback) and node selection (NodeSelectedCallback), and tree context (right-click) menu.
Unfortunately, it appears that the ExploreStruct utility has not been updated for compatibility with the latest Matlab releases. This causes numerous warning messages about not using ‘v0’ when calling uitree and uitreenode. I also had to add a call to drawnow following the uitree‘s creation in line #30, otherwise the tree did not appear (due to EDT issues [6]). Also, I had to comment out line #83 (set(root,’UIContextMenu’, cmenu);) which caused an error (“There is no ‘UIContextMenu’ property in the ‘com.mathworks.hg.peer.UITreeNode’ class”) – the tree’s context-menu actually works even without this line.
Finally, I had to fix line #182 so that the node icons will appear correctly:

%pth = [matlabroot, '\work\exp_struct_icons\'];  %old
pth = [fileparts(which(mfilename)) '\exp_struct_icons\'];  %new

ExploreStruct is not documented and does not have extensive error-checking that I would like to see in real-world applications. But it is relatively easy to read and understand due to its use of internal functions and meaningful variable names.
Altogether, aside from the minor nuances mentioned above, I believe that readers who are interested in implementing custom tree objects in their Matlab GUI can learn a lot from this utility, and you can easily adapt its code for your own needs (or if you can’t, I am always willing to help [7]).

Categories: GUI, Java, Medium risk of breaking in future versions, Semi-documented function, Stock Matlab function

5 Comments (Open | Close)

5 Comments To "An interesting uitree utility"

#1 Comment By Aurélien On April 4, 2011 @ 01:03

I often use this utility in my GUIs that I call directly from uimenu . It allows me to check very quickly the contents of my structures. I also added another cell in the right panel (like Name, Size , Class) to display the current value of selected item. Currently it is the best explorer of structure that I found on the FEX.

I am not really surprised that uitable , uitree are the most-popular search terms. IMHO, these features should have been officially implemented and documented in a MATLAB release for a while since they are really helpful.

#2 Comment By Amir On May 17, 2015 @ 01:48

Hello Yair,

First your blog is really useful and helpful.
I’m working on a GUI that contain a uitree, now I wanted to export that uitree to enable user to save it to an external variable.
but, there is known problem that the java class is not serializable.
Now I’m try to use that uitree utility in order to convert a struct to a uitree, but the utility is not working. (I’m working with R2015a release)
It getting me an error message:
“Error using set
The name ‘UserData’ is not an accessible property for an instance of class

Error in explorestruct (line 37)
set(tmp, ‘UserData’, cell_Data);”

It looks like there is no ‘UserData’ field for MJScrollPane, do you know why this field is unavailable and how to solve it?

#3 Comment By Yair Altman On May 17, 2015 @ 01:56

MathWorks removed the UserData property (and some other generic properties such as ApplicationData) from Java components a few releases ago. In general, code that relies on undocumented features cannot be expected to work forever, it may indeed break in some future release.

You will need to contact the utility’s creator to adapt it for new Matlab releases, or modify the relevant utility yourself, or hire a consultant to help you.

#4 Comment By Peter Cook On October 4, 2017 @ 02:09

I wrote this to fill a need I’ve had for a lightweight “structure explorer” type utility that lets me navigate a struct quickly without the need to necessarily plot the data. I create the tree as I traverse the structure so I didn’t have to make an ExpandFcn/MousePressedCallback; I also wrote a version that expands all nodes initially but it can be a bit overwhelming for a broad or deep tree (e.g. a struct returned by xml2struct). Maybe you or someone reading this page will find this useful as the original utility doesn’t work in recent MATLAB releases.

function mtree = mapStruct(s)

    hFig1 = figure('pos',[32,32,512,960]);
    hFig1.ToolBar = 'none';
    hFig1.MenuBar = 'none';
    hFig1.Name = 'Struct Explorer';
    hFig1.NumberTitle = 'off';

    root = uitreenode('v0','root','root',[],false);
    mtree = uitree('v0','Root',root,'Parent',hFig1);

    function structTraverse(s,node)
        f = fieldnames(s);
        %explore each field in the input struct s
        for kField = 1 : length(f)
            structField = s.(f{kField}); %structField = getfield(s,f{kField});
            %if struct field is a cell array of structs, convert to struct array IFF structs are identical
            if iscell(structField)
                if all(cellfun(@isstruct,structField))
                        structField = [structField{:}];
                        %add some sort of exception handling
            %if the structure field is also a struct explore each field
            if isstruct(structField) %strcmp(structFieldClass,'struct')
                %determine depth of struct field & enumerate if depth > 1
                structDepth = length(structField);
                if structDepth > 1
                    newNode = uitreenode('v0',f{kField},f{kField},[],false);
                    %if the depth of the structure field is greater than 1
                    %add a blank parent node then add each layer/depth level as a node in the tree
                    for kLayer = 1:structDepth
                        nodeLayerName = sprintf('%s(%d)',f{kField},kLayer);
                        nextNode = uitreenode('v0', nodeLayerName,nodeLayerName,[],false);
                    %if the depth of the structure field is 1 add it as a node
                    nextNode = uitreenode('v0',f{kField},f{kField},[],false);
                %make a node for the variable/attribute and display information about the variable
                newNode = uitreenode('v0',f{kField},f{kField},[],false);
                [nRow,nCol] = size(structField);
                structFieldClass = class(structField);
                if isempty(structField)
                    displayString = 'empty';
                elseif nRow==1 && ischar(structField)
                    displayString = structField;
                elseif nRow==1 && nCol==1 && any(strcmp(structFieldClass,{'single','double','logical'}))
                    displayString = sprintf('%f',structField);
                    displayString = sprintf('%dx%d %s',nRow,nCol,structFieldClass);
                nextNode = uitreenode('v0',displayString,displayString,[],true);
    mtree.Position(3) = 512;


#5 Comment By Yair Altman On October 4, 2017 @ 11:17

@Peter – Thanks for sharing

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/interesting-uitree-utility

URLs in this post:

[1] semi-documented: http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented

[2] here: http://undocumentedmatlab.com/blog/uitree/

[3] review: http://blogs.mathworks.com/pick/2011/03/25/looking-back-2005-in-review/

[4] Structure Explorer: http://www.mathworks.com/matlabcentral/fileexchange/7828-STRUCTURE%20EXPLORER

[5] Image: https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/7828/versions/1/screenshot.JPG

[6] due to EDT issues: http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/

[7] willing to help: http://undocumentedmatlab.com/consulting/

[8] Customizing uitree : https://undocumentedmatlab.com/articles/customizing-uitree

[9] Customizing uitree nodes – part 2 : https://undocumentedmatlab.com/articles/customizing-uitree-nodes-2

[10] uitree : https://undocumentedmatlab.com/articles/uitree

[11] Customizing uitree nodes – part 1 : https://undocumentedmatlab.com/articles/customizing-uitree-nodes

[12] ScreenCapture utility : https://undocumentedmatlab.com/articles/screencapture-utility

[13] Adding a context-menu to a uitree : https://undocumentedmatlab.com/articles/adding-context-menu-to-uitree

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.