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 thelbox2
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 calllbox2
in 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
In GUIDE, click theEditorbuttonto view the code.
Near the top of the Editor window, use theGo Tobutton to navigate to the functions discussed below.
lbox2_OpeningFcn
The callback functionlbox2_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 thelbox2
function.
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_OpeningFcn
arehObject
,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 thevarargin
input argument contains two additional items (suggesting that the user did specify a path). Thus, subsequentif
statements check to see whether the path is valid.
listbox1_callback
The callback functionlistbox1_callback
executes when the user clicks a list box item. This statement, near the beginning of the function, returnstrue
whenever the user double-clicks an item in the list box:
ifstrcmp(get(handles.figure1,'SelectionType'),'open')
true
, 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};
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