Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Interactive List Box App in GUIDE

This example shows how to examine and run a prebuilt GUIDE app. The app contains a list box that displays the files in a particular folder. When you double-click an item in the list, MATLAB® opens the item.

Open and Run The Example

To open the example in GUIDE, click the Open Script button on this page. To run the app and allow it to show the contents of the current folder, click the Run Figure (green play button) at the top of the GUIDE window.

Alternatively, you can call the lbox2 function in the Command Window with the 'dir' name-value pair argument. The name-value pair argument allows you to list the contents of any folder. For example, this command lists the files in the C:\ folder on a Windows® system:

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

Note: Before you can call lbox2 in the Command Window, you must save the GUIDE files in a folder on your MATLAB® path. To save the files, select File > Save As in GUIDE.

Examine the Layout and Callback Code

  1. In GUIDE, click the Editor button to view the code.

  2. Near the top of the Editor window, use the Go To button to navigate to the functions discussed below.

lbox2_OpeningFcn

The callback function lbox2_OpeningFcn executes just before the list box appears in the UI for the first time. The following statements determine whether the user specified a path argument to the lbox2 function.

if nargin == 3,
    initial_dir = pwd;
elseif nargin > 4
    if strcmpi(varargin{1},'dir')
        if exist(varargin{2},'dir')
            initial_dir = varargin{2};
        else
            errordlg('Input must be a valid directory','Input Argument Error!')
            return
        end
    else
        errordlg('Unrecognized input argument','Input Argument Error!');
        return;
    end
end
If nargin==3, then the only input arguments to lbox2_OpeningFcn are hObject, eventdata, and handles. Therefore, the user did not specify a path when they called lbox2, so the list box shows the contents of the current folder. If nargin>4, then the varargin input argument contains two additional items (suggesting that the user did specify a path). Thus, subsequent if statements check to see whether the path is valid.

listbox1_callback

The callback function listbox1_callback executes when the user clicks a list box item. This statement, near the beginning of the function, returns true whenever the user double-clicks an item in the list box:

if strcmp(get(handles.figure1,'SelectionType'),'open')
If that condition is true, then listbox1_callback determines which list box item the user selected:
index_selected = get(handles.listbox1,'Value');
file_list = get(handles.listbox1,'String');
filename = file_list{index_selected};
The rest of the code in this callback function determines how to open the selected item based on whether the item is a folder, FIG file, or another type of file:
    if  handles.is_dir(handles.sorted_index(index_selected))
        cd (filename)
        load_listbox(pwd,handles)
    else
        [path,name,ext] = fileparts(filename);
        switch ext
            case '.fig'
                guide (filename)
            otherwise
                try
                    open(filename)
                catch ex
                    errordlg(...
                      ex.getReport('basic'),'File Type Error','modal')
                end
        end
    end

Related Examples

Was this topic helpful?