Creating Graphical User Interfaces |
This example creates a subfunction to load items into the list box. This subfunction accepts the path to a directory and the handles
structure as input arguments. It performs these steps:
dir
command to get a list of files in the specified directory and to determine which name is a directory and which is a file. dir
returns a structure (dir_struct
) with two fields, name
and isdir
, which contain this information.
sortrows
) and save the sorted names and other information in the handles
structure so this information can be passed to other functions.
The name
structure field is passed to sortrows
as a cell array, which is transposed to get one file name per row. The isdir
field and the sorted index values, sorted_index
, are saved as vectors in the handles
structure.
guidata
to save the handles
structure.
String
property to display the file and directory names and set the Value
property to 1
. This is necessary to ensure Value
never exceeds the number of items in String
, since MATLAB updates the Value
property only when a selection occurs and not when the contents of String
changes.
String
property to the output of the pwd
command.
The load_listbox
function is called by the opening function of the GUI M-file 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)
The list box callback handles only one case: a double-click on 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 directory, the GUI changes to that directory and lists its contents.
Defining How to Open File Types
The callback makes use of the fact that the open
command can handle a number of different file types. However, the callback treats FIG-files differently. Instead of opening the FIG-file, it passes it to the guide
command for editing.
Determining Which Item the User Selected
Since a single click on an item also invokes the list box callback, it is necessary to query the figure SelectionType
property to determine when the user has performed a double click. A double-click on an item sets the SelectionType
property to open
.
All the items in the list box are referenced by an index from 1
to n
, where 1
refers to the first item and n
is the index of the n
th item. MATLAB 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.
Determining if the Selected Item is a File or Directory
The load_listbox function uses the dir
command to obtain a list of values that indicate whether an item is a file or directory. These values (1 for directory, 0 for file) are saved in the handles
structure. The list box callback queries these values to determine if current selection is a file or directory and takes the following action:
cd
) and call load_listbox
again to populate the list box with the contents of the new directory.
fileparts
) to determine if it is a FIG-file, which is opened with guide
. All other file types are passed to open
.
The open
statement is called within a try
/catch
block to capture errors in an error dialog (errordlg
), instead of returning to the command line.
function listbox1_Callback(hObject, eventdata, handles) if strcmp(get(handles.figure1,'SelectionType'),'open')% If double click
index_selected = get(handles.listbox1,'Value'); file_list = get(handles.listbox1,'String'); filename = file_list{index_selected};% Item selected in list box
if handles.is_dir(handles.sorted_index(index_selected))% If directory
cd (filename) load_listbox(pwd,handles)% Load list box with new directory
else [path,name,ext,ver] = fileparts(filename); switch ext case '.fig' guide (filename)% Open FIG-file with guide command
otherwise try open(filename)% Use open for other file types
catch errordlg(lasterr,'File Type Error','modal') end end end end
Opening Unknown File Types
You can extend the file types that the open
command recognizes to include any file having a three-character extension. You do this by creating an M-file with the name openxyz
, where xyz
is the extension. Note that the list box callback does not take this approach for FIG-files since openfig.m
is required by the GUI M-file. See open
for more information.
Specifying the Directory to List | Accessing Workspace Variables from a List Box |
© 1994-2005 The MathWorks, Inc.