Home > io > checkFileExistence.m

checkFileExistence

PURPOSE ^

checkFileExistence

SYNOPSIS ^

function files=checkFileExistence(files,fullOrTemp,allowSpace,checkExist)

DESCRIPTION ^

 checkFileExistence
   Check whether files exist. If no full path is given a file should be
   located in the current folder, which by default is appended to the
   filename.

   Input:
   files           string or cell array of strings with path to file(s) or
                   path or filename(s)
   fullOrTemp      0: do not change path to file(s)
                   1: return full path to file(s)
                   2: copy file(s) to system default temporary folder and
                      return full path
                   (opt, default 0)
   allowSpace      logical, whether 'space' character is allowed in the
                   path (opt, default true)
   checkExist      logical, whether file existence should really be
                   checked, as this function can also be used to return
                   the full path to a new file (opt, default true). Can
                   only be set to false if fullOrTemp is set to 1.

   Output:
   files           string or cell array of strings with updated paths if
                   fullOrTemp was set as 1 or 2, otherwise original paths
                   are returned
   
   Usage: files=checkFileExistence(files,fullOrTemp,allowSpace,checkExist)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function files=checkFileExistence(files,fullOrTemp,allowSpace,checkExist)
0002 % checkFileExistence
0003 %   Check whether files exist. If no full path is given a file should be
0004 %   located in the current folder, which by default is appended to the
0005 %   filename.
0006 %
0007 %   Input:
0008 %   files           string or cell array of strings with path to file(s) or
0009 %                   path or filename(s)
0010 %   fullOrTemp      0: do not change path to file(s)
0011 %                   1: return full path to file(s)
0012 %                   2: copy file(s) to system default temporary folder and
0013 %                      return full path
0014 %                   (opt, default 0)
0015 %   allowSpace      logical, whether 'space' character is allowed in the
0016 %                   path (opt, default true)
0017 %   checkExist      logical, whether file existence should really be
0018 %                   checked, as this function can also be used to return
0019 %                   the full path to a new file (opt, default true). Can
0020 %                   only be set to false if fullOrTemp is set to 1.
0021 %
0022 %   Output:
0023 %   files           string or cell array of strings with updated paths if
0024 %                   fullOrTemp was set as 1 or 2, otherwise original paths
0025 %                   are returned
0026 %
0027 %   Usage: files=checkFileExistence(files,fullOrTemp,allowSpace,checkExist)
0028 
0029 if nargin<2
0030     fullOrTemp = 0;
0031 end
0032 if nargin<3
0033     allowSpace = true;
0034 end
0035 if nargin<4
0036     checkExist = true;
0037 end
0038 files=convertCharArray(files);
0039 if numel(files)==1
0040     oneFile=true;
0041 else
0042     oneFile=false;
0043 end
0044 filesOriginal = files;
0045 
0046 %Make all full paths before check of file existence
0047 if ispc % full path starts like "C:\"
0048     inCurrDir = cellfun(@isempty,regexpi(files,'^[a-z]\:\\'));
0049 else %isunix full path starts like "/"
0050     inCurrDir = cellfun(@isempty,regexpi(files,'^\/'));
0051 end
0052 files(inCurrDir) = fullfile(cd,files(inCurrDir));
0053 
0054 %Check existence
0055 if checkExist
0056     for i=1:numel(files)
0057         if ~isfile(files{i})
0058             error('File "%s" cannot be found\n',files{i});
0059         elseif allowSpace == false & strfind(files{i},' ')
0060             error('File "%s" has an invalid space in the filename or path, please remove this before running this function\n',files{i});
0061         end
0062     end
0063 end
0064 
0065 switch fullOrTemp
0066     case 0
0067         files = filesOriginal;
0068     case 1
0069         % files already contains full path
0070     case 2
0071         for i=1:numel(files)
0072             tmpFile=tempname;
0073             copyfile(files{i},tmpFile);
0074             files{i}=tmpFile;
0075         end
0076 end
0077 
0078 if oneFile == true
0079     files = files{1};
0080 end

Generated by m2html © 2005