Main Content

Interactive List Box App in GUIDE

Note

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, seeGUIDE Migration Strategiesfor information on how to help maintain compatibility of the app with future MATLAB releases. To create new apps interactively,Develop Apps Using App Designerinstead.

这个例子展示了如何检查并运行一个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 theRun Figure(green play button) to run it.

Alternatively, you can call thelbox2function 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 calllbox2in the Command Window, you must save the GUIDE files in a folder on your MATLAB® path. To save the files, select文件>另存为in GUIDE.

Examine the Layout and Callback Code

  1. In GUIDE, click theEditorbuttonto view the code.

  2. Near the top of the Editor window, use theGo Tobutton to navigate to the functions discussed below.

lbox2_OpeningFcn

The callback functionlbox2_OpeningFcnexecutes 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 thelbox2function.

ifnargin == 3, initial_dir = pwd;elseifnargin > 4ifstrcmpi(varargin{1},'dir')ifexist(varargin{2},'dir') initial_dir = varargin{2};elseerrordlg('Input must be a valid directory','Input Argument Error!')returnendelseerrordlg('Unrecognized input argument','Input Argument Error!');return;endend
Ifnargin==3, then the only input arguments tolbox2_OpeningFcnarehObject,eventdata, and处理. Therefore, the user did not specify a path when they calledlbox2, so the list box shows the contents of the current folder. Ifnargin>4, then thevarargininput argument contains two additional items (suggesting that the user did specify a path). Thus, subsequentifstatements check to see whether the path is valid.

listbox1_callback

The callback functionlistbox1_callbackexecutes when the user clicks a list box item. This statement, near the beginning of the function, returnstruewhenever the user double-clicks an item in the list box:

ifstrcmp(get(handles.figure1,'SelectionType'),'open')
If that condition istrue, thenlistbox1_callbackdetermines 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处理.is_dir(handles.sorted_index(index_selected)) cd (filename) load_listbox(pwd,handles)else[path,name,ext] = fileparts(filename);switchextcase'.fig'guide (filename)otherwisetryopen(filename)catchex errordlg(...ex.getReport('basic'),'File Type Error','modal')endendend

Related Topics