time | Calls | line |
---|
| | 1 | function [validFileNames, fileExtensions] = validateFileName(filename, extensions)
|
| | 2 | %VALIDATEFILENAME Validate the existence of a specified file.
|
| | 3 | % Validate the existence of a file named FILENAME by searching through
|
| | 4 | % the filesystem. First, check if FILENAME is a valid absolute or
|
| | 5 | % relative path to a file. Otherwise, check for a file named FILENAME on
|
| | 6 | % the MATLAB path.
|
| | 7 | %
|
| | 8 | % varargin: Optionally, specify a list of file extensions to search for
|
| | 9 | % as a cell array of character vectors.
|
| | 10 | %
|
| | 11 | % For example, if FILENAME == 'myFile' and extensions == {'.txt', '.csv'},
|
| | 12 | % then VALIDATEFILENAME will check for the existence of the following
|
| | 13 | % files, in the order specified below:
|
| | 14 | %
|
| | 15 | % 1. 'myFile'
|
| | 16 | % 2. 'myFile.txt'
|
| | 17 | % 3. 'myFile.csv'
|
| | 18 | %
|
| | 19 | % VALIDATEFILENAME will return all file names matching the specified
|
| | 20 | % validation criteria as VALIDFILENAMES, along with their
|
| | 21 | % corresponding file extensions as FILEXTENSIONS.
|
| | 22 |
|
| | 23 | % Copyright 2016-2020 The MathWorks, Inc.
|
| | 24 |
|
< 0.001 | 2 | 25 | filename = convertCharsToStrings(filename);
|
| | 26 |
|
| | 27 | % Check if additional file extensions were provided. If so, generate
|
| | 28 | % additional file names for validation.
|
< 0.001 | 2 | 29 | if nargin > 1
|
< 0.001 | 2 | 30 | extensions = convertCharsToStrings(extensions);
|
< 0.001 | 2 | 31 | extensions(~startsWith(extensions,'.')) = "." + extensions(~startsWith(extensions,'.'));
|
0.004 | 2 | 32 | [~, ~, ext] = fileparts(filename);
|
< 0.001 | 2 | 33 | if ext == ""
|
| | 34 | additionalFileNames = filename + extensions;
|
| | 35 | fileNamesToBeValidated = [filename, additionalFileNames];
|
< 0.001 | 2 | 36 | else
|
< 0.001 | 2 | 37 | fileNamesToBeValidated = filename;
|
< 0.001 | 2 | 38 | end
|
| | 39 | else
|
| | 40 | fileNamesToBeValidated = filename;
|
< 0.001 | 2 | 41 | end
|
| | 42 |
|
| | 43 | % Validate file names.
|
< 0.001 | 2 | 44 | isFile = isfile(fileNamesToBeValidated);
|
< 0.001 | 2 | 45 | validFileNames = fileNamesToBeValidated(isFile);
|
< 0.001 | 2 | 46 | if any(~isFile,'all')
|
| | 47 | validFileNames = [validFileNames(:); validateFileNameOnMATLABPath(fileNamesToBeValidated(~isFile))];
|
< 0.001 | 2 | 48 | end
|
| | 49 |
|
| | 50 | % If no valid files were found, then error.
|
< 0.001 | 2 | 51 | if isempty(validFileNames)
|
| | 52 | error(message('MATLAB:textio:textio:FileNotFound', filename));
|
| 2 | 53 | end
|
| | 54 |
|
| | 55 |
|
0.001 | 2 | 56 | validFileNames = localUnique(validFileNames);
|
| | 57 | % Get file extensions of valid file names.
|
< 0.001 | 2 | 58 | [paths, ~, fileExtensions] = fileparts(validFileNames);
|
< 0.001 | 2 | 59 | hasNoPath = (strlength(paths)==0);
|
0.012 | 2 | 60 | validFileNames(hasNoPath) = fullfile(pwd,validFileNames(hasNoPath));
|
| | 61 | % Return only unique file names and extensions.
|
< 0.001 | 2 | 62 | fileExtensions = cellstr(localUnique(fileExtensions));
|
< 0.001 | 2 | 63 | validFileNames = cellstr(validFileNames);
|
< 0.001 | 2 | 64 | end
|
Other subfunctions in this file are not included in this listing.