User`s guide
10 Examples of GUIDE GUIs
• Use the dir command to get a list of files in the specified folder and to
determine which name is a folder and which is a file.
dir returns a
structure (
dir_struct)withtwofields,name an d isdir, which contain
this information.
• Sort th e file and fo ld er names (
sortrows) and save the sorted names and
other informa tion in th e
handles structure so that 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 nam e 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 folder names and set
the
Value property to 1, ensuring that Value never exceeds the number of
items in
String, because MATL AB software updates the Va lue property
only when a selection occurs; not wh en the contents of
String changes.
• 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_l istb ox(dir_path, handle s)
cd (dir_path )
dir_struct = dir(dir_path);
[sorted_names,sorted_index] = s ortrows({dir_struct.nam e}');
handles.file_names = sorted_nam es;
handles.is_dir = [dir_struct.isdir];
handles.sorted_index = sorted_i ndex;
guidata(handles.figure1,handles)
set(handles.listbox1,'String',handl es.file_names,...
'Value',1)
set(handles.text1,'String',pwd)
The List Box Callback
The list box callback 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 s elected item
10-58