Specifications

5 GUI Applications
5-12
Change to the specified directory so the GUI can navigate up and down the
tree as required.
Use the
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.
Sort the file and directory names (
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.
Call
guidata to save the handles structure.
Set the list box
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.
Displays the current directory 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 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
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