An interesting uitree utility

“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 uitree (starting here), and I am on a constant lookout for additional interesting angles on uitree to post here. Brett Shoelson’s recent review of 2005’s Pick-of-the-Week utilities, provided me with such an opportunity.

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

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). 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).

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

Tags: , , ,

Bookmark and SharePrint Print

5 Responses to An interesting uitree utility

  1. Aurélien says:

    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. Amir says:

    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?

    • 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.

  3. Peter Cook says:

    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

Leave a Reply

Your email address will not be published. Required fields are marked *