Creating Graphical User Interfaces | ![]() ![]() |
This example uses a separate 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 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 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 in the initialization section of the application M-file as well as 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)
![]() | Specifying the Directory to List | The List Box Callback | ![]() |