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 string 'create'
and a string containing the full path to the directory as arguments. The syntax for doing this is list_box('create','dir_path')
. If you do not specify a directory (i.e., if you call the GUI M-file with no input arguments), the GUI then uses the MATLAB current directory.
The default behavior of the GUI M-file that GUIDE generates is to run the GUI when there are no input arguments or to call 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
'create'
and second input argument is a string that specifies a valid path to a directory - run the GUI, displaying the specified directory.
The following code listing show the setup section of the GUI M-file, which does one the following:
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;% Update handles structure
guidata(hObject, handles); if nargin == 3, initial_dir = pwd; elseif nargin == 4 & exist(varargin{1},'dir') initial_dir = varargin{1}; else errordlg('Input argument must be a valid directory','Input Argument Error!') return end% Populate the listbox
load_listbox(initial_dir,handles)
List Box Directory Reader | Loading the List Box |