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

An interesting uitree utility

Posted By Yair Altman On March 30, 2011 @ 11:00 am In GUI,Java,Medium risk of breaking in future versions,Semi-documented function,Stock Matlab function | 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 [3] uitree (starting here [4]), and I am on a constant lookout for additional interesting angles on uitree to post here. Brett Shoelson’s recent review [5] of 2005’s Pick-of-the-Week utilities, provided me with such an opportunity.

One of 2005’s featured utilities, Structure Explorer [6] 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) [7]

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 [8]). 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 [9]).


5 Comments (Open | Close)

5 Comments To "An interesting uitree utility"

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

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 @ 1:48 am

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
‘com.mathworks.mwswing.MJScrollPane’.

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 @ 1:56 am

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 @ 2:09 am

Yair,
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))
                    try
                        structField = [structField{:}];
                    catch
                        %add some sort of exception handling
                    end
                end
            end
 
            %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);
                    node.add(newNode);
                    %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);
                        newNode.add(nextNode);
                        structTraverse(structField(kLayer),nextNode)
                    end
                else
                    %if the depth of the structure field is 1 add it as a node
                    nextNode = uitreenode('v0',f{kField},f{kField},[],false);
                    node.add(nextNode);
                    structTraverse(structField,nextNode)
                end
            else
                %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);
                else
                    displayString = sprintf('%dx%d %s',nRow,nCol,structFieldClass);
                end
                nextNode = uitreenode('v0',displayString,displayString,[],true);
                newNode.add(nextNode);
                node.add(newNode);
            end
        end
    end
 
    structTraverse(s,root)
    mtree.reloadNode(root)
    mtree.Position(3) = 512;
 
end

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

@Peter – Thanks for sharing


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

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

URLs in this post:

[1] Image: http://undocumentedmatlab.com/feed/

[2] email feed: http://undocumentedmatlab.com/subscribe_email.html

[3] semi-documented: https://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented

[4] here: https://undocumentedmatlab.com/blog/uitree/

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

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

[7] Image: http://www.mathworks.com/matlabcentral/fx_files/7828/1/fig2.JPG

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

[9] willing to help: https://undocumentedmatlab.com/consulting/

[10] Figure toolbar components : https://undocumentedmatlab.com/blog/figure-toolbar-components

[11] Customizing uitree : https://undocumentedmatlab.com/blog/customizing-uitree

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

[13] Uitab colors, icons and images : https://undocumentedmatlab.com/blog/uitab-colors-icons-images

[14] Uitab customizations : https://undocumentedmatlab.com/blog/uitab-customizations

[15] Uitable sorting : https://undocumentedmatlab.com/blog/uitable-sorting

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