Creating Graphical User Interfaces | ![]() ![]() |
Specifying the Directory to List
You can specify the directory to list when the GUI is first opened by passing the full pathname as a string input argument. If you do not specify a directory (i.e., if you call the application M-file with no input arguments), the GUI then uses MATLAB's current directory.
As generated, the application M-file launches the GUI when there are no input arguments and calls a subfunction when the first input argument is a character string. This example changes this behavior so that you can call the M-file with:
The following code listing show the entire initialization section of the application M-file. The statements in bold are the additions made to the generated code:
function varargout = lbox2(varargin)if
nargin <= 1
% LAUNCH GUI
if nargin == 0
initial_dir = pwd;
elseif nargin == 1 & exist(varargin{1},'dir')
initial_dir = varargin{1};
else
errordlg('Input argument must be a valid directory',...
'Input Argument Error!')
return
end
fig = openfig(mfilename,'reuse');% Use system color scheme for figure:
set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));% Generate a structure of handles to pass to callbacks, and store it.
handles = guihandles(fig); guidata(fig, handles);% Populate the listbox
load_listbox(varargin{1},handles)
if nargout > 0 varargout{1} = fig; end elseif ischar(varargin{1})% INVOKE NAMED SUBFUNCTION OR CALLBACK
try [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard catch disp(lasterr); end end
![]() | List Box Directory Reader | Loading the List Box | ![]() |