9 Examples of GUIDE UIs
- Determine whether 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 folder. These values (1 for folder, 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 folder and takes the following action:
- If the selection is a folder — change to the folder (cd) and call load_listbox
again to populate the list box with the contents of the new folder. - If the selection is a file — get the file extension (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 box (errordlg), instead of returning to the command line.
You can extend the file types that the open command recognizes to include any file
having a three-character extension. Do this by creating a MATLAB code file with the
name openxyz.m. xyz is the file extension for the type of files to be handled. Do not,
however, take this approach for opening FIG-files, because openfig.m is a MATLAB
function which is needed to open UIs. For more information, see open and openfig.
listbox1_Callback code
function listbox1_Callback(hObject, eventdata, handles)
get(handles.figure1,'SelectionType');
% If double click
if strcmp(get(handles.figure1,'SelectionType'),'open')
index_selected = get(handles.listbox1,'Value');
file_list = get(handles.listbox1,'String');
% Item selected in list box
filename = file_list{index_selected};
% If folder
if handles.is_dir(handles.sorted_index(index_selected))
cd (filename)
% Load list box with new folder.
load_listbox(pwd,handles)
else
[path,name,ext] = fileparts(filename);
switch ext
case '.fig'
% Open FIG-file with guide command.
guide (filename)
otherwise