Home > m > gui > gltpda > uipickfiles.m

uipickfiles

PURPOSE ^

uipickfiles: GUI program to select file(s) and/or directories.

SYNOPSIS ^

function out = uipickfiles(varargin)

DESCRIPTION ^

uipickfiles: GUI program to select file(s) and/or directories.

 Syntax:
   files = uipickfiles('PropertyName',PropertyValue,...)

 The current directory can be changed by operating in the file navigator:
 double-clicking on a directory in the list to move further down the tree,
 using the popup menu to move up the tree, typing a path in the box to
 move to any directory or right-clicking on the path box to revisit a
 previously-listed directory.

 Files can be added to the list by double-clicking or selecting files
 (non-contiguous selections are possible with the control key) and
 pressing the Add button.  Files in the list can be removed or re-ordered.
 When finished, a press of the Done button will return the full paths to
 the selected files in a cell array, structure array or character array.
 If the Cancel button is pressed then zero is returned.

 The following optional property/value pairs can be specified as arguments
 to control the indicated behavior:

   Property    Value
   ----------  ----------------------------------------------------------
   FilterSpec  String to specify starting directory and/or file filter.
               Ex:  'C:\bin' will start up in that directory.  '*.txt'
               will list only files ending in '.txt'.  'c:\bin\*.txt' will
               do both.  Default is to start up in the current directory
               and list all files.  Can be changed with the GUI.

   REFilter    String containing a regular expression used to filter the
               file list.  Ex: '\.m$|\.mat$' will list files ending in
               '.m' and '.mat'.  Default is empty string.  Can be used
               with FilterSpec and both filters are applied.  Can be
               changed with the GUI.

   Prompt      String containing a prompt appearing in the title bar of
               the figure.  Default is 'Select files'.

   NumFiles    Scalar or vector specifying number of files that must be
               selected. A scalar specifies an exact value; a two-element
               vector can be used to specify a range, [min max].  The
               function will not return unless the specified number of
               files have been chosen.  Default is [] which accepts any
               number of files.

   Output      String specifying the data type of the output: 'cell',
               'struct' or 'char'.  Specifying 'cell' produces a cell
               array of strings, the strings containing the full paths of
               the chosen files.  'Struct' returns a structure array like
               the result of the dir function except that the 'name' field
               contains a full path instead of just the file name.  'Char'
               returns a character array of the full paths.  This is most
               useful when you have just one file and want it in a string
               instead of a cell array containing just one string.  The
               default is 'cell'.

 All properties and values are case-insensitive and need only be
 unambiguous.  For example,

   files = uipickfiles('num',1,'out','ch')

 is valid usage.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function out = uipickfiles(varargin)
0002 %uipickfiles: GUI program to select file(s) and/or directories.
0003 %
0004 % Syntax:
0005 %   files = uipickfiles('PropertyName',PropertyValue,...)
0006 %
0007 % The current directory can be changed by operating in the file navigator:
0008 % double-clicking on a directory in the list to move further down the tree,
0009 % using the popup menu to move up the tree, typing a path in the box to
0010 % move to any directory or right-clicking on the path box to revisit a
0011 % previously-listed directory.
0012 %
0013 % Files can be added to the list by double-clicking or selecting files
0014 % (non-contiguous selections are possible with the control key) and
0015 % pressing the Add button.  Files in the list can be removed or re-ordered.
0016 % When finished, a press of the Done button will return the full paths to
0017 % the selected files in a cell array, structure array or character array.
0018 % If the Cancel button is pressed then zero is returned.
0019 %
0020 % The following optional property/value pairs can be specified as arguments
0021 % to control the indicated behavior:
0022 %
0023 %   Property    Value
0024 %   ----------  ----------------------------------------------------------
0025 %   FilterSpec  String to specify starting directory and/or file filter.
0026 %               Ex:  'C:\bin' will start up in that directory.  '*.txt'
0027 %               will list only files ending in '.txt'.  'c:\bin\*.txt' will
0028 %               do both.  Default is to start up in the current directory
0029 %               and list all files.  Can be changed with the GUI.
0030 %
0031 %   REFilter    String containing a regular expression used to filter the
0032 %               file list.  Ex: '\.m$|\.mat$' will list files ending in
0033 %               '.m' and '.mat'.  Default is empty string.  Can be used
0034 %               with FilterSpec and both filters are applied.  Can be
0035 %               changed with the GUI.
0036 %
0037 %   Prompt      String containing a prompt appearing in the title bar of
0038 %               the figure.  Default is 'Select files'.
0039 %
0040 %   NumFiles    Scalar or vector specifying number of files that must be
0041 %               selected. A scalar specifies an exact value; a two-element
0042 %               vector can be used to specify a range, [min max].  The
0043 %               function will not return unless the specified number of
0044 %               files have been chosen.  Default is [] which accepts any
0045 %               number of files.
0046 %
0047 %   Output      String specifying the data type of the output: 'cell',
0048 %               'struct' or 'char'.  Specifying 'cell' produces a cell
0049 %               array of strings, the strings containing the full paths of
0050 %               the chosen files.  'Struct' returns a structure array like
0051 %               the result of the dir function except that the 'name' field
0052 %               contains a full path instead of just the file name.  'Char'
0053 %               returns a character array of the full paths.  This is most
0054 %               useful when you have just one file and want it in a string
0055 %               instead of a cell array containing just one string.  The
0056 %               default is 'cell'.
0057 %
0058 % All properties and values are case-insensitive and need only be
0059 % unambiguous.  For example,
0060 %
0061 %   files = uipickfiles('num',1,'out','ch')
0062 %
0063 % is valid usage.
0064 
0065 % Version: 1.0, 25 April 2006
0066 % Author:  Douglas M. Schwarz
0067 % Email:   dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
0068 % Real_email = regexprep(Email,{'=','*'},{'@','.'})
0069 % $Id: uipickfiles.m,v 1.1 2008/03/01 13:43:20 nicola Exp $
0070 
0071 
0072 % Define properties and set default values.
0073 prop.filterspec = '*';
0074 prop.refilter = '';
0075 prop.prompt = 'Select files';
0076 prop.numfiles = [];
0077 prop.output = 'cell';
0078 
0079 % Process inputs and set prop fields.
0080 properties = fieldnames(prop);
0081 arg_index = 1;
0082 while arg_index <= nargin
0083     arg = varargin{arg_index};
0084     if ischar(arg)
0085         prop_index = find(strncmpi(arg,properties,length(arg)));
0086         if length(prop_index) == 1
0087             prop.(properties{prop_index}) = varargin{arg_index + 1};
0088         else
0089             error('Property ''%s'' does not exist or is ambiguous.',arg)
0090         end
0091         arg_index = arg_index + 2;
0092     elseif isstruct(arg)
0093         arg_fn = fieldnames(arg);
0094         for i = 1:length(arg_fn)
0095             prop_index = find(strncmpi(arg_fn{i},properties,...
0096                 length(arg_fn{i})));
0097             if length(prop_index) == 1
0098                 prop.(properties{prop_index}) = arg.(arg_fn{i});
0099             else
0100                 error('Property ''%s'' does not exist or is ambiguous.',...
0101                     arg_fn{i})
0102             end
0103         end
0104         arg_index = arg_index + 1;
0105     else
0106         error(['Properties must be specified by property/value pairs',...
0107             ' or structures.'])
0108     end
0109 end
0110 
0111 % Validate FilterSpec property.
0112 if isempty(prop.filterspec)
0113     prop.filterspec = '*';
0114 end
0115 if ~ischar(prop.filterspec)
0116     error('FilterSpec property must contain a string.')
0117 end
0118 
0119 % Validate REFilter property.
0120 if ~ischar(prop.refilter)
0121     error('REFilter property must contain a string.')
0122 end
0123 
0124 % Validate Prompt property.
0125 if ~ischar(prop.prompt)
0126     error('Prompt property must contain a string.')
0127 end
0128 
0129 % Validate NumFiles property.
0130 if numel(prop.numfiles) > 2 || any(prop.numfiles < 0)
0131     error('NumFiles must be empty, a scalar or two-element vector.')
0132 end
0133 prop.numfiles = unique(prop.numfiles);
0134 if isequal(prop.numfiles,1)
0135     numstr = 'Select exactly 1 file.';
0136 elseif length(prop.numfiles) == 1
0137     numstr = sprintf('Select exactly %d files.',prop.numfiles);
0138 else
0139     numstr = sprintf('Select %d to %d files.',prop.numfiles);
0140 end
0141 
0142 % Validate Output property.
0143 legal_outputs = {'cell','struct','char'};
0144 out_idx = find(strncmpi(prop.output,legal_outputs,length(prop.output)));
0145 if length(out_idx) == 1
0146     prop.output = legal_outputs{out_idx};
0147 else
0148     error(['Value of ''Output'' property, ''%s'', is illegal or '...
0149         'ambiguous.'],prop.output)
0150 end
0151 
0152 
0153 % Initialize file lists.
0154 [current_dir,f,e] = fileparts(prop.filterspec);
0155 filter = [f,e];
0156 if isempty(current_dir)
0157     current_dir = pwd;
0158 end
0159 if isempty(filter)
0160     filter = '*';
0161 end
0162 re_filter = prop.refilter;
0163 full_filter = fullfile(current_dir,filter);
0164 path_cell = path2cell(current_dir);
0165 fdir = filtered_dir(full_filter,re_filter);
0166 filenames = {fdir.name}';
0167 filenames = annotate_file_names(filenames,fdir);
0168 
0169 % Initialize some data.
0170 file_picks = {};
0171 full_file_picks = {};
0172 dir_picks = dir(' ');  % Create empty directory structure.
0173 show_full_path = false;
0174 nodupes = true;
0175 history = {current_dir};
0176 
0177 % Create figure.
0178 gray = get(0,'DefaultUIControlBackgroundColor');
0179 fig = figure('Position',[0 0 740 445],...
0180     'Color',gray,...
0181     'WindowStyle','modal',...
0182     'Resize','off',...
0183     'NumberTitle','off',...
0184     'Name',prop.prompt,...
0185     'IntegerHandle','off',...
0186     'CloseRequestFcn',@cancel,...
0187     'CreateFcn',{@movegui,'center'});
0188 
0189 % Create uicontrols.
0190 uicontrol('Style','frame',...
0191     'Position',[255 260 110 70])
0192 uicontrol('Style','frame',...
0193     'Position',[275 135 110 100])
0194 
0195 navlist = uicontrol('Style','listbox',...
0196     'Position',[10 10 250 320],...
0197     'String',filenames,...
0198     'Value',[],...
0199     'BackgroundColor','w',...
0200     'Callback',@clicknav,...
0201     'Max',2);
0202 pickslist = uicontrol('Style','listbox',...
0203     'Position',[380 10 350 320],...
0204     'String',{},...
0205     'BackgroundColor','w',...
0206     'Callback',@clickpicks,...
0207     'Max',2);
0208 
0209 openbut = uicontrol('Style','pushbutton',...
0210     'Position',[270 300 80 20],...
0211     'String','Open',...
0212     'Enable','off',...
0213     'Callback',@open);
0214 arrow = [2 2 2 2 2 2 2 2 1 2 2 2;...
0215          2 2 2 2 2 2 2 2 2 0 2 2;...
0216          2 2 2 2 2 2 2 2 2 2 0 2;...
0217          0 0 0 0 0 0 0 0 0 0 0 0;...
0218          2 2 2 2 2 2 2 2 2 2 0 2;...
0219          2 2 2 2 2 2 2 2 2 0 2 2;...
0220          2 2 2 2 2 2 2 2 1 2 2 2];
0221 arrow(arrow == 2) = NaN;
0222 arrow_im = NaN*ones(16,76);
0223 arrow_im(6:12,45:56) = arrow/2;
0224 im = repmat(arrow_im,[1 1 3]);
0225 addbut = uicontrol('Style','pushbutton',...
0226     'Position',[270 270 80 20],...
0227     'String','Add    ',...
0228     'Enable','off',...
0229     'CData',im,...
0230     'Callback',@add);
0231 
0232 removebut = uicontrol('Style','pushbutton',...
0233     'Position',[290 205 80 20],...
0234     'String','Remove',...
0235     'Enable','off',...
0236     'Callback',@remove);
0237 moveupbut = uicontrol('Style','pushbutton',...
0238     'Position',[290 175 80 20],...
0239     'String','Move Up',...
0240     'Enable','off',...
0241     'Callback',@moveup);
0242 movedownbut = uicontrol('Style','pushbutton',...
0243     'Position',[290 145 80 20],...
0244     'String','Move Down',...
0245     'Enable','off',...
0246     'Callback',@movedown);
0247 
0248 uicontrol('Position',[10 380 250 16],...
0249     'Style','text',...
0250     'String','Current Directory',...
0251     'HorizontalAlignment','center')
0252 dir_popup = uicontrol('Style','popupmenu',...
0253     'Position',[10 335 250 20],...
0254     'BackgroundColor','w',...
0255     'String',path_cell(end:-1:1),...
0256     'Value',1,...
0257     'Callback',@dirpopup);
0258 hist_cm = uicontextmenu;
0259 pathbox = uicontrol('Position',[10 360 250 20],...
0260     'Style','edit',...
0261     'BackgroundColor','w',...
0262     'String',current_dir,...
0263     'HorizontalAlignment','left',...
0264     'Callback',@change_path,...
0265     'UIContextMenu',hist_cm);
0266 hist_menus = [];
0267 hist_cb = @history_cb;
0268 hist_menus = make_history_cm(hist_cb,hist_cm,hist_menus,history);
0269 
0270 uicontrol('Position',[10 425 80 16],...
0271     'Style','text',...
0272     'String','File Filter',...
0273     'HorizontalAlignment','left')
0274 uicontrol('Position',[100 425 160 16],...
0275     'Style','text',...
0276     'String','Reg. Exp. Filter',...
0277     'HorizontalAlignment','left')
0278 showallfiles = uicontrol('Position',[270 405 100 20],...
0279     'Style','checkbox',...
0280     'String','Show All Files',...
0281     'Value',0,...
0282     'HorizontalAlignment','left',...
0283     'Callback',@togglefilter);
0284 filter_ed = uicontrol('Position',[10 405 80 20],...
0285     'Style','edit',...
0286     'BackgroundColor','w',...
0287     'String',filter,...
0288     'HorizontalAlignment','left',...
0289     'Callback',@setfilspec);
0290 refilter_ed = uicontrol('Position',[100 405 160 20],...
0291     'Style','edit',...
0292     'BackgroundColor','w',...
0293     'String',re_filter,...
0294     'HorizontalAlignment','left',...
0295     'Callback',@setrefilter);
0296 
0297 viewfullpath = uicontrol('Style','checkbox',...
0298     'Position',[380 335 230 20],...
0299     'String','Show full paths',...
0300     'Value',show_full_path,...
0301     'HorizontalAlignment','left',...
0302     'Callback',@showfullpath);
0303 remove_dupes = uicontrol('Style','checkbox',...
0304     'Position',[380 360 230 20],...
0305     'String','Remove duplicates (as per full path)',...
0306     'Value',nodupes,...
0307     'HorizontalAlignment','left',...
0308     'Callback',@removedupes);
0309 uicontrol('Position',[380 405 350 20],...
0310     'Style','text',...
0311     'String','Selected Files',...
0312     'HorizontalAlignment','center')
0313 uicontrol('Position',[280 80 80 30],'String','Done',...
0314     'Callback',@done);
0315 uicontrol('Position',[280 30 80 30],'String','Cancel',...
0316     'Callback',@cancel);
0317 
0318 if ~isempty(prop.numfiles)
0319     uicontrol('Position',[380 385 350 16],...
0320         'Style','text',...
0321         'String',numstr,...
0322         'ForegroundColor','r',...
0323         'HorizontalAlignment','center')
0324 end
0325 
0326 set(fig,'HandleVisibility','off')
0327 
0328 uiwait(fig)
0329 
0330 % Compute desired output.
0331 switch prop.output
0332     case 'cell'
0333         out = full_file_picks;
0334     case 'struct'
0335         out = dir_picks(:);
0336     case 'char'
0337         out = char(full_file_picks);
0338     case 'cancel'
0339         out = 0;
0340 end
0341 
0342 % -------------------- Callback functions --------------------
0343 
0344     function add(varargin)
0345         values = get(navlist,'Value');
0346         for i = 1:length(values)
0347             dir_pick = fdir(values(i));
0348             pick = dir_pick.name;
0349             pick_full = fullfile(current_dir,pick);
0350             dir_pick.name = pick_full;
0351             if ~nodupes || ~any(strcmp(full_file_picks,pick_full))
0352                 file_picks{end + 1} = pick;
0353                 full_file_picks{end + 1} = pick_full;
0354                 dir_picks(end + 1) = dir_pick;
0355             end
0356         end
0357         if show_full_path
0358             set(pickslist,'String',full_file_picks,'Value',[]);
0359         else
0360             set(pickslist,'String',file_picks,'Value',[]);
0361         end
0362         set([removebut,moveupbut,movedownbut],'Enable','off');
0363     end
0364 
0365     function remove(varargin)
0366         values = get(pickslist,'Value');
0367         file_picks(values) = [];
0368         full_file_picks(values) = [];
0369         dir_picks(values) = [];
0370         top = get(pickslist,'ListboxTop');
0371         num_above_top = sum(values < top);
0372         top = top - num_above_top;
0373         num_picks = length(file_picks);
0374         new_value = min(min(values) - num_above_top,num_picks);
0375         if num_picks == 0
0376             new_value = [];
0377             set([removebut,moveupbut,movedownbut],'Enable','off')
0378         end
0379         if show_full_path
0380             set(pickslist,'String',full_file_picks,'Value',new_value,...
0381                 'ListboxTop',top)
0382         else
0383             set(pickslist,'String',file_picks,'Value',new_value,...
0384                 'ListboxTop',top)
0385         end
0386     end
0387 
0388     function open(varargin)
0389         values = get(navlist,'Value');
0390         if fdir(values).isdir
0391             if strcmp(fdir(values).name,'.')
0392                 return
0393             elseif strcmp(fdir(values).name,'..')
0394                 set(dir_popup,'Value',min(2,length(path_cell)))
0395                 dirpopup();
0396                 return
0397             end
0398             current_dir = fullfile(current_dir,fdir(values).name);
0399             history{end+1} = current_dir;
0400             history = unique(history);
0401             hist_menus = make_history_cm(hist_cb,hist_cm,hist_menus,...
0402                 history);
0403             full_filter = fullfile(current_dir,filter);
0404             path_cell = path2cell(current_dir);
0405             fdir = filtered_dir(full_filter,re_filter);
0406             filenames = {fdir.name}';
0407             filenames = annotate_file_names(filenames,fdir);
0408             set(dir_popup,'String',path_cell(end:-1:1),'Value',1)
0409             set(pathbox,'String',current_dir)
0410             set(navlist,'ListboxTop',1,'Value',[],'String',filenames)
0411             set(addbut,'Enable','off')
0412             set(openbut,'Enable','off')
0413         end
0414     end
0415 
0416     function clicknav(varargin)
0417         value = get(navlist,'Value');
0418         nval = length(value);
0419         dbl_click_fcn = @add;
0420         switch nval
0421             case 0
0422                 set([addbut,openbut],'Enable','off')
0423             case 1
0424                 set(addbut,'Enable','on');
0425                 if fdir(value).isdir
0426                     set(openbut,'Enable','on')
0427                     dbl_click_fcn = @open;
0428                 else
0429                     set(openbut,'Enable','off')
0430                 end
0431             otherwise
0432                 set(addbut,'Enable','on')
0433                 set(openbut,'Enable','off')
0434         end
0435         if strcmp(get(fig,'SelectionType'),'open')
0436             dbl_click_fcn();
0437         end
0438     end
0439 
0440     function clickpicks(varargin)
0441         value = get(pickslist,'Value');
0442         if isempty(value)
0443             set([removebut,moveupbut,movedownbut],'Enable','off')
0444         else
0445             set(removebut,'Enable','on')
0446             if min(value) == 1
0447                 set(moveupbut,'Enable','off')
0448             else
0449                 set(moveupbut,'Enable','on')
0450             end
0451             if max(value) == length(file_picks)
0452                 set(movedownbut,'Enable','off')
0453             else
0454                 set(movedownbut,'Enable','on')
0455             end
0456         end
0457         if strcmp(get(fig,'SelectionType'),'open')
0458             remove();
0459         end
0460     end
0461 
0462     function dirpopup(varargin)
0463         value = get(dir_popup,'Value');
0464         len = length(path_cell);
0465         path_cell = path_cell(1:end-value+1);
0466         if ispc && value == len
0467             current_dir = '';
0468             full_filter = filter;
0469             fdir = struct('name',getdrives,'date',datestr(now),...
0470                 'bytes',0,'isdir',1);
0471         else
0472             current_dir = cell2path(path_cell);
0473             history{end+1} = current_dir;
0474             history = unique(history);
0475             hist_menus = make_history_cm(hist_cb,hist_cm,hist_menus,...
0476                 history);
0477             full_filter = fullfile(current_dir,filter);
0478             fdir = filtered_dir(full_filter,re_filter);
0479         end
0480         filenames = {fdir.name}';
0481         filenames = annotate_file_names(filenames,fdir);
0482         set(dir_popup,'String',path_cell(end:-1:1),'Value',1)
0483         set(pathbox,'String',current_dir)
0484         set(navlist,'String',filenames,'Value',[])
0485         set(addbut,'Enable','off')
0486     end
0487 
0488     function change_path(varargin)
0489         proposed_path = get(pathbox,'String');
0490         % Process any directories named '..'.
0491         proposed_path_cell = path2cell(proposed_path);
0492         ddots = strcmp(proposed_path_cell,'..');
0493         ddots(find(ddots) - 1) = true;
0494         proposed_path_cell(ddots) = [];
0495         proposed_path = cell2path(proposed_path_cell);
0496         % Check for existance of directory.
0497         if ~exist(proposed_path,'dir')
0498             uiwait(errordlg(['Directory "',proposed_path,...
0499                 '" does not exist.'],'','modal'))
0500             return
0501         end
0502         current_dir = proposed_path;
0503         history{end+1} = current_dir;
0504         history = unique(history);
0505         hist_menus = make_history_cm(hist_cb,hist_cm,hist_menus,history);
0506         full_filter = fullfile(current_dir,filter);
0507         path_cell = path2cell(current_dir);
0508         fdir = filtered_dir(full_filter,re_filter);
0509         filenames = {fdir.name}';
0510         filenames = annotate_file_names(filenames,fdir);
0511         set(dir_popup,'String',path_cell(end:-1:1),'Value',1)
0512         set(pathbox,'String',current_dir)
0513         set(navlist,'String',filenames,'Value',[])
0514         set(addbut,'Enable','off')
0515         set(openbut,'Enable','off')
0516     end
0517 
0518     function showfullpath(varargin)
0519         show_full_path = get(viewfullpath,'Value');
0520         if show_full_path
0521             set(pickslist,'String',full_file_picks)
0522         else
0523             set(pickslist,'String',file_picks)
0524         end
0525     end
0526 
0527     function removedupes(varargin)
0528         nodupes = get(remove_dupes,'Value');
0529         if nodupes
0530             num_picks = length(full_file_picks);
0531             [unused,rev_order] = unique(full_file_picks(end:-1:1));
0532             order = sort(num_picks + 1 - rev_order);
0533             full_file_picks = full_file_picks(order);
0534             file_picks = file_picks(order);
0535             if show_full_path
0536                 set(pickslist,'String',full_file_picks,'Value',[])
0537             else
0538                 set(pickslist,'String',file_picks,'Value',[])
0539             end
0540             set([removebut,moveupbut,movedownbut],'Enable','off')
0541         end
0542     end
0543 
0544     function moveup(varargin)
0545         value = get(pickslist,'Value');
0546         set(removebut,'Enable','on')
0547         n = length(file_picks);
0548         omega = 1:n;
0549         index = zeros(1,n);
0550         index(value - 1) = omega(value);
0551         index(setdiff(omega,value - 1)) = omega(setdiff(omega,value));
0552         file_picks = file_picks(index);
0553         full_file_picks = full_file_picks(index);
0554         value = value - 1;
0555         if show_full_path
0556             set(pickslist,'String',full_file_picks,'Value',value)
0557         else
0558             set(pickslist,'String',file_picks,'Value',value)
0559         end
0560         if min(value) == 1
0561             set(moveupbut,'Enable','off')
0562         end
0563         set(movedownbut,'Enable','on')
0564     end
0565 
0566     function movedown(varargin)
0567         value = get(pickslist,'Value');
0568         set(removebut,'Enable','on')
0569         n = length(file_picks);
0570         omega = 1:n;
0571         index = zeros(1,n);
0572         index(value + 1) = omega(value);
0573         index(setdiff(omega,value + 1)) = omega(setdiff(omega,value));
0574         file_picks = file_picks(index);
0575         full_file_picks = full_file_picks(index);
0576         value = value + 1;
0577         if show_full_path
0578             set(pickslist,'String',full_file_picks,'Value',value)
0579         else
0580             set(pickslist,'String',file_picks,'Value',value)
0581         end
0582         if max(value) == n
0583             set(movedownbut,'Enable','off')
0584         end
0585         set(moveupbut,'Enable','on')
0586     end
0587 
0588     function togglefilter(varargin)
0589         value = get(showallfiles,'Value');
0590         if value
0591             filter = '*';
0592             re_filter = '';
0593             set([filter_ed,refilter_ed],'Enable','off')
0594         else
0595             filter = get(filter_ed,'String');
0596             re_filter = get(refilter_ed,'String');
0597             set([filter_ed,refilter_ed],'Enable','on')
0598         end
0599         full_filter = fullfile(current_dir,filter);
0600         fdir = filtered_dir(full_filter,re_filter);
0601         filenames = {fdir.name}';
0602         filenames = annotate_file_names(filenames,fdir);
0603         set(navlist,'String',filenames,'Value',[])
0604         set(addbut,'Enable','off')
0605     end
0606 
0607     function setfilspec(varargin)
0608         filter = get(filter_ed,'String');
0609         if isempty(filter)
0610             filter = '*';
0611             set(filter_ed,'String',filter)
0612         end
0613         % Process file spec if a subdirectory was included.
0614         [p,f,e] = fileparts(filter);
0615         if ~isempty(p)
0616             newpath = fullfile(current_dir,p,'');
0617             set(pathbox,'String',newpath)
0618             filter = [f,e];
0619             if isempty(filter)
0620                 filter = '*';
0621             end
0622             set(filter_ed,'String',filter)
0623             change_path();
0624         end
0625         full_filter = fullfile(current_dir,filter);
0626         fdir = filtered_dir(full_filter,re_filter);
0627         filenames = {fdir.name}';
0628         filenames = annotate_file_names(filenames,fdir);
0629         set(navlist,'String',filenames,'Value',[])
0630         set(addbut,'Enable','off')
0631     end
0632 
0633     function setrefilter(varargin)
0634         re_filter = get(refilter_ed,'String');
0635         fdir = filtered_dir(full_filter,re_filter);
0636         filenames = {fdir.name}';
0637         filenames = annotate_file_names(filenames,fdir);
0638         set(navlist,'String',filenames,'Value',[])
0639         set(addbut,'Enable','off')
0640     end
0641 
0642     function done(varargin)
0643         % Optional shortcut: click on a file and press 'Done'.
0644 %         if isempty(full_file_picks) && strcmp(get(addbut,'Enable'),'on')
0645 %             add();
0646 %         end
0647         numfiles = length(full_file_picks);
0648         if ~isempty(prop.numfiles)
0649             if numfiles < prop.numfiles(1)
0650                 msg = {'Too few files selected.',numstr};
0651                 uiwait(errordlg(msg,'','modal'))
0652                 return
0653             elseif numfiles > prop.numfiles(end)
0654                 msg = {'Too many files selected.',numstr};
0655                 uiwait(errordlg(msg,'','modal'))
0656                 return
0657             end
0658         end
0659         delete(fig)
0660     end
0661 
0662     function cancel(varargin)
0663         prop.output = 'cancel';
0664         delete(fig)
0665     end
0666 
0667     function history_cb(varargin)
0668         current_dir = history{varargin{3}};
0669         full_filter = fullfile(current_dir,filter);
0670         path_cell = path2cell(current_dir);
0671         fdir = filtered_dir(full_filter,re_filter);
0672         filenames = {fdir.name}';
0673         filenames = annotate_file_names(filenames,fdir);
0674         set(dir_popup,'String',path_cell(end:-1:1),'Value',1)
0675         set(pathbox,'String',current_dir)
0676         set(navlist,'ListboxTop',1,'Value',[],'String',filenames)
0677         set(addbut,'Enable','off')
0678         set(openbut,'Enable','off')
0679     end
0680 end
0681 
0682 
0683 % -------------------- Subfunctions --------------------
0684 
0685 function c = path2cell(p)
0686 % Turns a path string into a cell array of path elements.
0687 c = strread(p,'%s','delimiter','\\/');
0688 if ispc
0689     c = [{'My Computer'};c];
0690 else
0691     c = [{filesep};c(2:end)];
0692 end
0693 end
0694 
0695 
0696 function p = cell2path(c)
0697 % Turns a cell array of path elements into a path string.
0698 if ispc
0699     p = fullfile(c{2:end},'');
0700 else
0701     p = fullfile(c{:},'');
0702 end
0703 end
0704 
0705 
0706 function d = filtered_dir(full_filter,re_filter)
0707 % Like dir, but applies filters and sorting.
0708 p = fileparts(full_filter);
0709 if isempty(p) && full_filter(1) == '/'
0710     p = '/';
0711 end
0712 if exist(full_filter,'dir')
0713     c = cell(0,1);
0714     dfiles = struct('name',c,'date',c,'bytes',c,'isdir',c);
0715 else
0716     dfiles = dir(full_filter);
0717 end
0718 if ~isempty(dfiles)
0719     dfiles([dfiles.isdir]) = [];
0720 end
0721 ddir = dir(p);
0722 ddir = ddir([ddir.isdir]);
0723 % Additional regular expression filter.
0724 if nargin > 1 && ~isempty(re_filter)
0725     if ispc
0726         no_match = cellfun('isempty',regexpi({dfiles.name},re_filter));
0727     else
0728         no_match = cellfun('isempty',regexp({dfiles.name},re_filter));
0729     end
0730     dfiles(no_match) = [];
0731 end
0732 % Set navigator style:
0733 %    1 => mix file and directory names
0734 %    2 => means list all files before all directories
0735 %    3 => means list all directories before all files
0736 %    4 => same as 2 except put . and .. directories first
0737 if isunix
0738     style = 4;
0739 else
0740     style = 4;
0741 end
0742 switch style
0743     case 1
0744         d = [dfiles;ddir];
0745         [unused,index] = sort({d.name});
0746         d = d(index);
0747     case 2
0748         [unused,index1] = sort({dfiles.name});
0749         [unused,index2] = sort({ddir.name});
0750         d = [dfiles(index1);ddir(index2)];
0751     case 3
0752         [unused,index1] = sort({dfiles.name});
0753         [unused,index2] = sort({ddir.name});
0754         d = [ddir(index2);dfiles(index1)];
0755     case 4
0756         [unused,index1] = sort({dfiles.name});
0757         dot1 = find(strcmp({ddir.name},'.'));
0758         dot2 = find(strcmp({ddir.name},'..'));
0759         ddot1 = ddir(dot1);
0760         ddot2 = ddir(dot2);
0761         ddir([dot1,dot2]) = [];
0762         [unused,index2] = sort({ddir.name});
0763         d = [ddot1;ddot2;dfiles(index1);ddir(index2)];
0764 end
0765 end
0766 
0767 
0768 function drives = getdrives
0769 % Returns a cell array of drive names on Windows.
0770 letters = char('A':'Z');
0771 num_letters = length(letters);
0772 drives = cell(1,num_letters);
0773 for i = 1:num_letters
0774     if exist([letters(i),':\'],'dir');
0775         drives{i} = [letters(i),':'];
0776     end
0777 end
0778 drives(cellfun('isempty',drives)) = [];
0779 end
0780 
0781 
0782 function filenames = annotate_file_names(filenames,dir_listing)
0783 % Adds a trailing filesep character to directory names.
0784 fs = filesep;
0785 for i = 1:length(filenames)
0786     if dir_listing(i).isdir
0787         filenames{i} = [filenames{i},fs];
0788     end
0789 end
0790 end
0791 
0792 
0793 function hist_menus = make_history_cm(cb,hist_cm,hist_menus,history)
0794 % Make context menu for history.
0795 if ~isempty(hist_menus)
0796     delete(hist_menus)
0797 end
0798 num_hist = length(history);
0799 hist_menus = zeros(1,num_hist);
0800 for i = 1:num_hist
0801     hist_menus(i) = uimenu(hist_cm,'Label',history{i},...
0802         'Callback',{cb,i});
0803 end
0804 end

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003