time | Calls | line |
---|
| | 1 | function [pathstr, name, ext] = fileparts(file)
|
| | 2 | %FILEPARTS Filename parts.
|
| | 3 | % [FILEPATH,NAME,EXT] = FILEPARTS(FILE) returns the path, file name, and file name
|
| | 4 | % extension for the specified FILE. The FILE input is the name of a file or folder,
|
| | 5 | % and can include a path and file name extension. The function interprets all
|
| | 6 | % characters following the right-most path delimiter as a file name plus extension.
|
| | 7 | %
|
| | 8 | % If the FILE input consists of a folder name only, be sure that the right-most
|
| | 9 | % character is a path delimiter (/ or \). Otherwise, FILEPARTS parses the trailing
|
| | 10 | % portion of FILE as the name of a file and returns it in NAME instead of in
|
| | 11 | % FILEPATH.
|
| | 12 | %
|
| | 13 | % FILEPARTS only parses file names. It does not verify that the file or folder
|
| | 14 | % exists.
|
| | 15 | %
|
| | 16 | % To reconstruct a file name from the output of FILEPARTS, use STRCAT to
|
| | 17 | % concatenate the file name and the extension that begins with a period (.)
|
| | 18 | % without a path separator. Then, use FULLFILE to build the file name with
|
| | 19 | % the platform-dependent file separators where necessary.
|
| | 20 | % For example, fullfile(filepath, strcat(name,ext)).
|
| | 21 | %
|
| | 22 | % FILEPARTS is platform dependent. On Microsoft Windows systems, you can
|
| | 23 | % use either forward (/) or back (\) slashes as path delimiters, even within
|
| | 24 | % the same path. On Unix and Macintosh systems, use only / as a delimiter.
|
| | 25 | %
|
| | 26 | % See also FULLFILE, PATHSEP, FILESEP.
|
| | 27 |
|
| | 28 | % Copyright 1984-2019 The MathWorks, Inc.
|
| | 29 |
|
< 0.001 | 36 | 30 | pathstr = '';
|
< 0.001 | 36 | 31 | name = '';
|
< 0.001 | 36 | 32 | ext = '';
|
< 0.001 | 36 | 33 | inputWasString = false;
|
| | 34 |
|
| | 35 |
|
< 0.001 | 36 | 36 | if ~ischar(file) && ~isStringScalar(file)
|
| | 37 | error(message('MATLAB:fileparts:MustBeChar'));
|
< 0.001 | 36 | 38 | elseif isempty(file)
|
| | 39 | return;
|
< 0.001 | 36 | 40 | elseif ~isrow(file)
|
| | 41 | error(message('MATLAB:fileparts:MustBeChar'));
|
< 0.001 | 36 | 42 | end
|
| | 43 |
|
< 0.001 | 36 | 44 | if isstring(file)
|
| | 45 | inputWasString = true;
|
| | 46 | file = char(file);
|
< 0.001 | 36 | 47 | end
|
| | 48 |
|
< 0.001 | 36 | 49 | if ispc
|
| | 50 | ind = find(file == '/' | file == '\', 1, 'last');
|
| | 51 | if isempty(ind)
|
| | 52 | ind = find(file == ':', 1, 'last');
|
| | 53 | if ~isempty(ind)
|
| | 54 | pathstr = file(1:ind);
|
| | 55 | end
|
| | 56 | else
|
| | 57 | if ind == 2 && (file(1) == '/' || file(1) == '\')
|
| | 58 | % Special case for UNC server
|
| | 59 | pathstr = file;
|
| | 60 | ind = length(file);
|
| | 61 | else
|
| | 62 | pathstr = file(1:ind-1);
|
| | 63 | end
|
| | 64 | end
|
| | 65 | if isempty(ind)
|
| | 66 | name = file;
|
| | 67 | else
|
| | 68 | if ~isempty(pathstr) && pathstr(end)== ':'
|
| | 69 | % Don't append to D: which is a volume path on windows
|
| | 70 | if length(pathstr) > 2
|
| | 71 | pathstr = [pathstr filesep];
|
| | 72 | elseif length(file) >= 3 && (file(3) == '/' || file(3) == '\')
|
| | 73 | pathstr = [pathstr file(3)];
|
| | 74 | end
|
| | 75 | elseif isempty(deblank(pathstr))
|
| | 76 | pathstr = filesep;
|
| | 77 | end
|
| | 78 | name = file(ind+1:end);
|
| | 79 | end
|
< 0.001 | 36 | 80 | else % UNIX
|
< 0.001 | 36 | 81 | ind = find(file == filesep, 1, 'last');
|
< 0.001 | 36 | 82 | if isempty(ind)
|
| | 83 | name = file;
|
< 0.001 | 36 | 84 | else
|
< 0.001 | 36 | 85 | pathstr = file(1:ind-1);
|
| | 86 |
|
| | 87 | % Do not forget to add filesep when in the root filesystem
|
< 0.001 | 36 | 88 | if isempty(deblank(pathstr))
|
| | 89 | pathstr = filesep;
|
< 0.001 | 36 | 90 | end
|
< 0.001 | 36 | 91 | name = file(ind+1:end);
|
< 0.001 | 36 | 92 | end
|
< 0.001 | 36 | 93 | end
|
| | 94 |
|
< 0.001 | 36 | 95 | if ~isempty(name)
|
| | 96 | % Look for EXTENSION part
|
< 0.001 | 36 | 97 | ind = find(name == '.', 1, 'last');
|
| | 98 |
|
< 0.001 | 36 | 99 | if ~isempty(ind)
|
< 0.001 | 36 | 100 | ext = name(ind:end);
|
< 0.001 | 36 | 101 | name(ind:end) = [];
|
< 0.001 | 36 | 102 | end
|
< 0.001 | 36 | 103 | end
|
| | 104 |
|
< 0.001 | 36 | 105 | if inputWasString
|
| | 106 | pathstr = string(pathstr);
|
| | 107 | name = string(name);
|
| | 108 | ext = string(ext);
|
< 0.001 | 36 | 109 | end
|
Other subfunctions in this file are not included in this listing.