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.

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 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'名称值对参数。名称值对参数允许您列出任何文件夹的内容。例如,此命令列出了Windows®系统上的C:\ folder中的文件:

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文件>另存为在指南中。

Examine the Layout and Callback Code

  1. In GUIDE, click theEditorbuttonto view the code.

  2. 靠近编辑器窗口的顶部,使用Go Tobutton to navigate to the functions discussed below.

lbox2_OpeningFcn

回调函数lbox2_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
如果nargin==3, then the only input arguments tolbox2_OpeningFcnarehObject,eventdata, and处理. Therefore, the user did not specify a path when they calledlbox2,因此列表框显示当前文件夹的内容。如果nargin>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.

回调函数listbox1_callback.executes when the user clicks a list box item. This statement, near the beginning of the function, returns真正whenever the user double-clicks an item in the list box:

ifstrcmp(get(handles.figure1,'SelectionType'),'open')
如果that condition is真正, thenlistbox1_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:
ifhandles.is_dir(handles.sorted_index(index_selected))CD(文件名)Load_ListBox(PWD,Handles)else[path,name,ext] = fileparts(filename);开关extcase'.fig'guide (filename)otherwise尝试人事处en(filename)catchex errordlg(...ex.getReport('basic'),'File Type Error','modal')endendend

相关话题