Sunday, March 26, 2017

[Matlab GUI] Create a hierarchical tree node and get a node name selected

In Matlab GUI, a hierarchical tree node can be created using the built-in function "uitree"and "uitreenode".

---- Source URL: http://undocumentedmatlab.com/blog/uitree
The uitree is based on an underlying Java component, and accepts an optional figure handles followed y P-V (property-value) pairs. The uitree properties are Root, ExpandFcn, SelectionChangeFcn, Position (also Parent). A "v0" input argument are necessary to suppress a warning message.
Note that uitree is always created as a direct child of the containing figure, ignoring creation-time Parent values.
--------------------------------------------------------------------------------------

The following example shows how to create a uitree and get a selected node name using Matlab GUIDE.
Download example files: uitreeCreate_DisplayName.fig and uitreeCreate_DisplayName.m

1. Create a GUIDE template

























2. Create a uitree in the OpeningFcn function

% --- Executes just before uitreeCreate_DisplayName is made visible.
function uitreeCreate_DisplayName_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to uitreeCreate_DisplayName (see VARARGIN)

% Create a hierarchical tree for Fruits and Vegetables
% 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);

% Create a uitree on a specified GUIDE figure
mtree = uitree('v0',handles.figure1, 'Root', root);
% Specify the size of a uitree handles
set(mtree,'pos',[30,30,200,300]);
% Save the handle "mtree" to global handle structure
handles.mtree = mtree;
% Choose default command line output for uitreeCreate_DisplayName
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

3. Display a selected node name on the edit
After clicking the pushbutton, a selected node name will be displayed on the edit
Therefore, the function to display a selected node name will be created in the pushbutton callback function.

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Load the mtree handle
mtree = handles.mtree;
% a=mtree.getSelectedNodes;
nodename=mtree.SelectedNodes;
nName = nodename(1);

%value =nName.getValue(); % Currently, may ignore this.
% Get a selected node name
name = char(nName.getName());
% Get a parent name of a selected one
parentnode = nName.getParent();
parentname = char(parentnode.getName());

modelpath = [parentname,'/',name];
% Display a selected node and parent name on the edit
set(handles.edit1,'String',modelpath);

4. Result







2 comments:

  1. How can I use this without using a push button?
    something like a function or callback

    ReplyDelete