time | calls | line |
---|
| | 1 | function [pathstr, name, ext] = fileparts(file)
|
| | 2 | %FILEPARTS Filename parts.
|
| | 3 | % [PATHSTR,NAME,EXT] = FILEPARTS(FILE) returns the path, file name, and
|
| | 4 | % file name extension for the specified FILE. The FILE input is a string
|
| | 5 | % containing the name of a file or folder, and can include a path and
|
| | 6 | % file name extension. The function interprets all characters following
|
| | 7 | % the right-most path delimiter as a file name plus extension.
|
| | 8 | %
|
| | 9 | % If the FILE input consists of a folder name only, be sure that the
|
| | 10 | % right-most character is a path delimiter (/ or \). Othewise, FILEPARTS
|
| | 11 | % parses the trailing portion of FILE as the name of a file and returns
|
| | 12 | % it in NAME instead of in PATHSTR.
|
| | 13 | %
|
| | 14 | % FILEPARTS only parses file names. It does not verify that the file or
|
| | 15 | % folder exists. You can reconstruct the file from the parts using
|
| | 16 | % fullfile(pathstr,[name ext])
|
| | 17 | %
|
| | 18 | % FILEPARTS is platform dependent.
|
| | 19 | %
|
| | 20 | % On Microsoft Windows systems, you can use either forward (/) or back
|
| | 21 | % (\) slashes as path delimiters, even within the same string. On Unix
|
| | 22 | % and Macintosh systems, use only / as a delimiter.
|
| | 23 | %
|
| | 24 | % See also FULLFILE, PATHSEP, FILESEP.
|
| | 25 |
|
| | 26 | % Copyright 1984-2012 The MathWorks, Inc.
|
| | 27 |
|
| 1 | 28 | pathstr = '';
|
| 1 | 29 | name = '';
|
| 1 | 30 | ext = '';
|
| | 31 |
|
| 1 | 32 | if ~ischar(file)
|
| | 33 | error(message('MATLAB:fileparts:MustBeChar'));
|
| 1 | 34 | elseif isempty(file) % isrow('') returns false, do this check first
|
| | 35 | return;
|
| 1 | 36 | elseif ~isrow(file)
|
| | 37 | error(message('MATLAB:fileparts:MustBeChar'));
|
| | 38 | end
|
| | 39 |
|
| 1 | 40 | if ispc
|
| | 41 | ind = find(file == '/'|file == '\', 1, 'last');
|
| | 42 | if isempty(ind)
|
| | 43 | ind = find(file == ':', 1, 'last');
|
| | 44 | if ~isempty(ind)
|
| | 45 | pathstr = file(1:ind);
|
| | 46 | end
|
| | 47 | else
|
| | 48 | if ind == 2 && (file(1) == '\' || file(1) == '/')
|
| | 49 | %special case for UNC server
|
| | 50 | pathstr = file;
|
| | 51 | ind = length(file);
|
| | 52 | else
|
| | 53 | pathstr = file(1:ind-1);
|
| | 54 | end
|
| | 55 | end
|
| | 56 | if isempty(ind)
|
| | 57 | name = file;
|
| | 58 | else
|
| | 59 | if ~isempty(pathstr) && pathstr(end)==':' && ...
|
| | 60 | (length(pathstr)>2 || (length(file) >=3 && file(3) == '\'))
|
| | 61 | %don't append to D: like which is volume path on windows
|
| | 62 | pathstr = [pathstr '\'];
|
| | 63 | elseif isempty(deblank(pathstr))
|
| | 64 | pathstr = '\';
|
| | 65 | end
|
| | 66 | name = file(ind+1:end);
|
| | 67 | end
|
| 1 | 68 | else % UNIX
|
| 1 | 69 | ind = find(file == '/', 1, 'last');
|
| 1 | 70 | if isempty(ind)
|
| | 71 | name = file;
|
| 1 | 72 | else
|
| 1 | 73 | pathstr = file(1:ind-1);
|
| | 74 |
|
| | 75 | % Do not forget to add filesep when in the root filesystem
|
| 1 | 76 | if isempty(deblank(pathstr))
|
| | 77 | pathstr = '/';
|
| | 78 | end
|
| 1 | 79 | name = file(ind+1:end);
|
| 1 | 80 | end
|
| 1 | 81 | end
|
| | 82 |
|
| 1 | 83 | if isempty(name)
|
| 1 | 84 | return;
|
| | 85 | end
|
| | 86 |
|
| | 87 | % Look for EXTENSION part
|
| | 88 | ind = find(name == '.', 1, 'last');
|
| | 89 |
|
| | 90 | if isempty(ind)
|
| | 91 | return;
|
| | 92 | else
|
| | 93 | ext = name(ind:end);
|
| | 94 | name(ind:end) = [];
|
| | 95 | end
|