iputfile_jd

PURPOSE ^

SYNOPSIS ^

function [selectname,pathname] = iputfile_jd(opt_gui,filterspec,message,presel);

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.