MATLAB Creating Graphical User Interfaces

(Barry) #1
Interactive List Box in a GUIDE UI


  • Displays the current folder in the text box by setting its String property to the
    output of the pwd command.


The load_listbox function is called by the opening function, as well as by the list box
callback.


function load_listbox(dir_path, handles)
cd (dir_path)
dir_struct = dir(dir_path);
[sorted_names,sorted_index] = sortrows({dir_struct.name}');
handles.file_names = sorted_names;
handles.is_dir = [dir_struct.isdir];
handles.sorted_index = sorted_index;
guidata(handles.figure1,handles)
set(handles.listbox1,'String',handles.file_names,...
'Value',1)
set(handles.text1,'String',pwd)


Code List Box Behavior


The listbox1_Callback code handles only one case: a double-click of an item. Double
clicking is the standard way to open a file from a list box. If the selected item is a file, it
is passed to the open command; if it is a folder, the program navigates to that folder and
lists the contents.



  • Define how to open file types


The open command can handle a number of different file types, however, the callback
treats FIG-files differently. Instead of opening the FIG-file as a standalone figure, it
opens it with guide for editing.


  • Determine which item was selected


Since a single click of an item also invokes the list box callback, you must query the
figure SelectionType property to determine when you have performed a double
click. A double-click of an item sets the SelectionType property to open.

All the items in the list box are referenced by an index from 1 to n. A value of 1 refers
to the first item, and a value of n is the index of the nth item. The software saves this
index in the list box Value property.

The callback uses this index to get the name of the selected item from the list of items
contained in the String property.
Free download pdf