MATLAB Creating Graphical User Interfaces

(Barry) #1

9 Examples of GUIDE UIs


% Update handles structure
guidata(hObject, handles);
if nargin == 3,
initial_dir = pwd;
elseif nargin > 4
if strcmpi(varargin{1},'dir')
if exist(varargin{2},'dir')
initial_dir = varargin{2};
else
errordlg({'Input argument must be a valid',...
'folder'},'Input Argument Error!')
return
end
else
errordlg('Unrecognized input argument',...
'Input Argument Error!');
return;
end
end
% Populate the listbox
load_listbox(initial_dir,handles)

Load the List Box

A local function loads items into the list box. This local function accepts the path to a
folder and the handles structure as input arguments and performs these steps:


  • Changes to the specified folder so that the code can navigate up and down the tree, as
    required.

  • Uses the dir command to get a list of files in the specified folder and to determine
    which name is a folder and which is a file. dir returns a structure (dir_struct)
    with two fields, name and isdir, containing this information.

  • Sorts the file and folder names (sortrows) and saves the sorted names and other
    information in the handles structure so that this information can be passed to other
    functions.


The name structure field is passed to sortrows as a cell array, which is transposed
to get one file name per row. The isdir field and the sorted index values,
sorted_index, are saved as vectors in the handles structure.


  • Calls guidata to save the handles structure.

  • Sets the list box String property to display the file and folder names and set the
    Value property to 1 , ensuring that Value never exceeds the number of items in
    String, because MATLAB software updates the Value property only when a
    selection occurs; not when the contents of String changes.

Free download pdf