Main Content

Interactive List Box App in GUIDE


The GUIDE environment will be removed in a future release. After GUIDE is removed, existing GUIDE apps will continue to run in MATLAB® but they will not be editable in GUIDE.

To continue editing an existing GUIDE app, see GUIDE Migration Strategies for information on how to help maintain compatibility of the app with future MATLAB releases. To create new apps interactively, Develop Apps Using App Designer instead.

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

Open the app in GUIDE, and click the Run Figure (green play button) to run it.

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:


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.


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};
            errordlg('Input must be a valid directory','Input Argument Error!')
        errordlg('Unrecognized input argument','Input Argument Error!');
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.


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)
        [path,name,ext] = fileparts(filename);
        switch ext
            case '.fig'
                guide (filename)
                catch ex
                      ex.getReport('basic'),'File Type Error','modal')

Related Topics