0001 function [selectname,pathname] = igetfile_jd(opt_gui,filterspec,message,presel,multiselect);
0002 
0003 if nargin < 5,
0004     multiselect = 0;
0005 end
0006 
0007 if nargin < 4,
0008     presel = '';
0009 end
0010 
0011 if nargin < 3,
0012     message = 'Please select a file';
0013 end
0014 
0015 if nargin < 2,
0016     filterspec = '*';
0017 end
0018 
0019 if nargin < 1,
0020     opt_gui = 1;
0021 end
0022 
0023 if opt_gui,
0024     
0025     if multiselect == 0;
0026         
0027         [selectname,pathname] = uigetfile(filterspec,message,presel);
0028         
0029     else
0030         
0031         [selectname,pathname] = uigetfile(filterspec,message,presel,'MultiSelect','on');
0032         
0033     end
0034 else
0035     
0036     if ~isempty(presel),
0037         
0038         if exist(presel,'dir'),
0039             pathname = presel;
0040             if pathname(end) ~= filesep,
0041                 pathname = [pathname,filesep];
0042             end
0043             filename = '';
0044         else
0045             
0046             ipath = max(find(presel(1:end-1) == '/'));
0047             
0048             if isempty(ipath),
0049                 pathname = [pwd,'/'];
0050                 ipath = 0;
0051             else
0052                 pathname = presel(1:ipath);
0053             end
0054             
0055             filename = presel(ipath+1:end);
0056             
0057             if ~exist(presel,'file'),
0058                 if ~exist(pathname,'dir'),
0059                     
0060                     disp(['WARNING : the directory ''',pathname,''' does not exist.'])
0061                     
0062                     pathname = [pwd,'/'];
0063                     filename = '';
0064                     
0065                 else
0066                     
0067                     disp(['WARNING : the file ''',filename,''' does not exist in directory ''',pathname,'''.'])
0068                     
0069                     filename = '';
0070                     
0071                 end
0072             end
0073             
0074         end
0075         
0076     else
0077         pathname = [pwd,'/'];
0078         filename = '';        
0079     end
0080     
0081     spec = {};
0082     
0083     if ~isempty(filterspec),
0084         if iscell(filterspec),
0085             filterspec = filterspec(:,1);
0086         else
0087             filterspec = {filterspec};
0088         end
0089         
0090         iispec = 0;
0091         iistr = 1;
0092         for ispec = 1:length(filterspec),
0093             if iistr >= 1,
0094                 iispec = iispec + 1;
0095             end
0096             
0097             istar = [0,find(filterspec{ispec} == '*'),length(filterspec{ispec})+1];
0098             
0099             iistr = 0;
0100             for istr = 1:length(istar)-1,
0101                 if istar(istr) + 1 <= istar(istr+1) - 1,
0102                     iistr = iistr + 1;
0103                     spec{iispec}{iistr} = filterspec{ispec}(istar(istr) + 1:istar(istr+1) - 1);
0104                 end
0105             end
0106         end
0107     end
0108     
0109     selectname = '';
0110     
0111     while isempty(selectname);
0112         disp(' ');
0113         
0114         disp(message);
0115         
0116         files = dir(pathname);
0117         
0118         isel = 0;
0119         select = {};
0120         
0121         for ifile = 1:length(files),
0122             if files(ifile).isdir == 0,
0123                 
0124                 sel = 1;
0125                 
0126                 for ispec = 1:length(spec),
0127                     
0128                     sel = 1;
0129                     
0130                     istar = length(files(ifile).name) + 1;
0131                     
0132                     for istr = length(spec{ispec}):-1:1,
0133                         istar = strfind(files(ifile).name(1:istar-1),spec{ispec}{istr});
0134                         if isempty(istar),
0135                             sel = 0;
0136                             break
0137                         end
0138                     end
0139                     
0140                     if sel == 1,
0141                         break
0142                     end
0143                     
0144                 end                           
0145                 
0146                 if sel == 1,
0147                     select = [select,files(ifile).name];
0148                     if strcmp(files(ifile).name,filename),
0149                         isel = length(select);
0150                     end
0151                 end
0152                 
0153             end
0154         end
0155         
0156         if isempty(select),
0157             
0158             disp(' ');
0159             disp(['-----> No file with requested format in the directory ''',pathname,''' :'])
0160             
0161             isel_new = 0;
0162             
0163         else                             
0164             
0165             disp(' ');
0166             
0167             if multiselect == 0;
0168                 disp(['-----> Select a file from this list in the directory ''',pathname,''' :']);
0169             else
0170                 disp(['-----> Select files from this list in the directory ''',pathname,''' :']);
0171             end
0172             
0173             disp(' ');
0174             
0175         end
0176         
0177         for ifile = 1:length(select),
0178             disp(['[',num2str(ifile),'] : ',select{ifile}])
0179         end
0180         
0181         disp(' ');
0182         disp('[0] Change directory');
0183         disp(' ');
0184         disp('[-1] Cancel');
0185         disp(' ');
0186         
0187         isel_new = input(['? [',num2str(isel),'] : ']);
0188         
0189         if isempty(isel_new),
0190             isel_new = isel;
0191         end
0192         
0193         if isel_new == -1,
0194             
0195             selectname = 0;
0196             
0197         elseif isel_new == 0,
0198             
0199             pathname_new = igetdir_jd(0,message,pathname);
0200             
0201             if pathname_new ~= 0,
0202                 pathname = pathname_new;
0203             end
0204             
0205         else
0206             if multiselect == 0 & isnumeric(isel_new) & length(isel_new) == 1 & any(isel_new == 1:length(select));
0207                 
0208                 selectname = select{isel_new};
0209                 
0210             elseif multiselect == 1 & isnumeric(isel_new) & all(sum(isel_new(:)*(1./[1:length(select)]) == ones(length(isel_new),length(select)),2)),
0211                 for iisel = 1:length(isel_new),
0212                     selectname{iisel} = select{isel_new(iisel)};
0213                 end
0214             else
0215                 disp(['-----> Invalid option']);
0216             end
0217         end
0218         
0219     end
0220 end
0221