MATLAB Creating Graphical User Interfaces

(Barry) #1
Interactive List Box in a GUIDE UI

(^2) From GUIDE Layout Editor, click the Editor button.
The lbox2.m code displays in the MATLAB Editor.
Implement the List Box
The following sections describe the implementation:



  • “Specify the Folder” on page 9-47 — shows how to pass a folder path as an input
    argument when the program runs.

  • “Load the List Box” on page 9-48 — describes the local function that loads the
    contents of the folder into the list box. This local function also saves information about
    the contents of a folder in the handles structure.

  • “Code List Box Behavior” on page 9-49 — describes how the list box is coded to
    respond to double clicks on items in the list box.


Specify the Folder


By default, UI code files generated by GUIDE open the UI when there are no input
arguments, and call a local function 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 lbox2 program to display the
contents of 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 code from lbox2.m shows the code for lbox2_OpeningFcn, which sets the
list box folder to:



  • The current folder, if no folder is specified.

  • The specified folder, if a folder is specified.


function lbox2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for lbox2
handles.output = hObject;
Free download pdf