Skip to Main Content Skip to Search
Product Documentation

List Box Directory Reader (GUIDE)

About the List Box Directory Example

This example uses a list box to display the files in a folder. When the user double clicks a list item, one of the following happens:

The following figure illustrates the GUI.

folder list GUI for C:\\GUI\\tools

View and Run the List Box Directory GUI

If you are reading this document in the MATLAB Help browser, you can access the example FIG-file and code file by clicking the following links. If you are reading this on the Web or in PDF form, go to the corresponding section in the MATLAB Help Browser to use the links.

If you intend to modify the layout or code of this GUI example, first save a copy of its code file and FIG-file to your current folder (You need write access to your current folder to do this.) Follow these steps to copy the example files to your current folder and then to open them:

  1. Click here to copy the files to your current folder

  2. Type guide lbox2 or click here to open the FIG-file in GUIDE

  3. Type edit lbox2 or click here to open the code file in the Editor

You can view the properties of any component by double-clicking it in the Layout Editor to open the Property Inspector for it. You can modify either the figure, the code, or both, and then save the GUI in your current folder using File > Save as from GUIDE. This saves both files, allowing you to rename them, if you choose.

To just inspect the GUI in GUIDE and run it, follow these steps instead:

  1. Click here to add the example files to the MATLAB path (only for the current session).

  2. Click here to run the lbox2 GUI.

  3. Click here to display the GUI in the GUIDE Layout Editor (read only).

  4. Click here to display the GUI code file in the MATLAB Editor (read only).

Implement the List Box Directory GUI

The following sections describe the implementation:

Specify the Directory

By default, GUI code files generated by GUIDE open the GUI when there are no input arguments, and call a subfunction when the first input argument is a character string. This example changes the behavior so that if you put the example files, lbox2.m and lbox2.fig, on the MATLAB path you can run the GUI displaying a particular folder. To do so, pass the dir function as a string for the first input argument, and a string that specifies the path to the folder for the second input argument. For instance, from the Command Window, run the following to have the list box display the files in C:\myfiles:

lbox2('dir','C:\my_files')

The following listing from lbox2.m shows the code for GUI initialization, which sets the list box folder to:

Load the List Box

This example uses a subfunction to load items into the list box. This subfunction accepts the path to a folder and the handles structure as input arguments and performs these steps:

The load_listbox function is called by the opening function, 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 of 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 folder, the GUI changes to that folder and lists its contents.

Define How to Open File Types.  The open command can handle a number of different file types, however, the callback treats FIG-files differently. Instead of opening the FIG-file as a standalone figure, it opens it with guide for editing.

Determine Which Item the User Selected.  Since a single click of an item also invokes the list box callback, you must query the figure SelectionType property to determine when the user has performed a double click. A double-click of an item sets the SelectionType property to open.

All the items in the list box are referenced by an index from 1 to n. 1 refers to the first item and n is the index of the nth item. The software 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.

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:

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.

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
                try
                    % Use open for other file types.
                    open(filename)
                catch ex
                    errordlg(...
                      ex.getReport('basic'),'File Type Error','modal')
                end
        end
    end
end

Open Unknown File Types.  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 GUIs. For more information, see open and openfig.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS