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

3 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

    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.

Leave a Reply

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