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 file(s) or folder(s),
|
| | 5 | % and can include path and file name extensions. The FILE can be a row vector of characters
|
| | 6 | % or a string scalar(for a single FILE), or a cellstr, or a string matrix (for multiple FILEs).
|
| | 7 | % The function interprets all characters following 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 right-most
|
| | 10 | % character is a path delimiter (/ or \). Otherwise, FILEPARTS parses the trailing
|
| | 11 | % portion of FILE as the name of a file and returns it in NAME instead of in
|
| | 12 | % FILEPATH.
|
| | 13 | %
|
| | 14 | % FILEPARTS only parses file names. It does not verify that the file or folder
|
| | 15 | % exists.
|
| | 16 | %
|
| | 17 | % To reconstruct a file name from the output of FILEPARTS, use STRCAT to
|
| | 18 | % concatenate the file name and the extension that begins with a period (.)
|
| | 19 | % without a path separator. Then, use FULLFILE to build the file name with
|
| | 20 | % the platform-dependent file separators where necessary.
|
| | 21 | % For example, fullfile(filepath, strcat(name,ext)).
|
| | 22 | %
|
| | 23 | % FILEPARTS is platform dependent. On Microsoft Windows systems, you can
|
| | 24 | % use either forward (/) or back (\) slashes as path delimiters, even within
|
| | 25 | % the same path. On Unix and Macintosh systems, use only / as a delimiter.
|
| | 26 | %
|
| | 27 | % Examples
|
| | 28 | % To get parts of File Name for Windows :
|
| | 29 | % [filepath,name,ext] = fileparts('H:\user4\matlab\myfile.txt')
|
| | 30 | %
|
| | 31 | % To get parts of File Name for Linux :
|
| | 32 | % [filepath,name,ext] = fileparts('/home/jsmith/.cshrc')
|
| | 33 | %
|
| | 34 | % To get fileparts for multiple files :
|
| | 35 | % files = ["/home/jsmith/myfile.txt"; ...
|
| | 36 | % "/home/jdoe/subfolder/myOtherFile.m"];
|
| | 37 | % [filepath,name,ext] = fileparts(files)
|
| | 38 | %
|
| | 39 | % or,
|
| | 40 | % files = {'/home/jsmith/myfile.txt'; ...
|
| | 41 | % '/home/jdoe/subfolder/myOtherFile.m'};
|
| | 42 | % [filepath,name,ext] = fileparts(files)
|
| | 43 | %
|
| | 44 | % See also FULLFILE, PATHSEP, FILESEP.
|
| | 45 |
|
| | 46 | % Copyright 1984-2020 The MathWorks, Inc.
|
| | 47 |
|
| | 48 | % initial checks to see that input is well-formed and acceptable
|
< 0.001 | 8 | 49 | emptyFile = isempty(file);
|
< 0.001 | 8 | 50 | fileIsCellstr = iscellstr(file); %#ok<ISCLSTR>
|
< 0.001 | 8 | 51 | fileIsString = isstring(file);
|
< 0.001 | 8 | 52 | fileIsChar = ischar(file);
|
| | 53 |
|
< 0.001 | 8 | 54 | supportedTypes = fileIsChar || fileIsCellstr || fileIsString;
|
| | 55 |
|
< 0.001 | 8 | 56 | if ~supportedTypes
|
| | 57 | % error when input is not any of char or string or cellstr
|
| | 58 | error(message("MATLAB:fileparts:MustBeChar"));
|
< 0.001 | 8 | 59 | elseif fileIsChar && ~isrow(file) && ~emptyFile
|
| | 60 | % error for char matrices
|
| | 61 | error(message("MATLAB:fileparts:MustBeChar"));
|
< 0.001 | 8 | 62 | elseif iscell(file) && ~fileIsCellstr && emptyFile
|
| | 63 | % error for empty cells
|
| | 64 | error(message("MATLAB:fileparts:MustBeChar"));
|
< 0.001 | 8 | 65 | elseif fileIsString && any(ismissing(file),"all")
|
| | 66 | % error for missing string
|
| | 67 | error(message("MATLAB:fileparts:StringMissingUnsupported"));
|
< 0.001 | 8 | 68 | end
|
| | 69 |
|
| | 70 | % if input is empty, return the appropriate output
|
< 0.001 | 8 | 71 | if emptyFile
|
| | 72 | if fileIsString
|
| | 73 | % string output
|
| | 74 | pathstr = string.empty(size(file));
|
| | 75 | name = string.empty(size(file));
|
| | 76 | ext = string.empty(size(file));
|
| | 77 | else
|
| | 78 | if fileIsChar
|
| | 79 | % char output
|
| | 80 | pathstr = char.empty(size(file));
|
| | 81 | name = char.empty(size(file));
|
| | 82 | ext = char.empty(size(file));
|
| | 83 | else
|
| | 84 | % cellstr output
|
| | 85 | pathstr = cell.empty(size(file));
|
| | 86 | name = cell.empty(size(file));
|
| | 87 | ext = cell.empty(size(file));
|
| | 88 | end
|
| | 89 | end
|
| | 90 | return;
|
< 0.001 | 8 | 91 | end
|
| | 92 |
|
| | 93 | % branch for scalar input vs vector input on Windows
|
< 0.001 | 8 | 94 | inputIsScalar = (fileIsChar && ~fileIsCellstr) || (fileIsString && isscalar(file));
|
< 0.001 | 8 | 95 | if inputIsScalar && ispc
|
0.004 | 8 | 96 | [pathstr, name, ext] = legacyPCExecution(file);
|
| | 97 | else
|
| | 98 | % convert to string to use string API
|
| | 99 | file = string(file);
|
| | 100 |
|
| | 101 | % Branch code for OS-specific constraints
|
| | 102 | if ispc
|
| | 103 | % convert input into a column vector
|
| | 104 | [pathstr, name] = pcExecution(file);
|
| | 105 | else
|
| | 106 | % convert input into a row vector
|
| | 107 | [pathstr, name] = unixExecution(file);
|
| | 108 | end
|
| | 109 |
|
| | 110 | % separate file name from extension
|
| | 111 | numOutArgs = nargout;
|
| | 112 | if numOutArgs > 1
|
| | 113 | [ext, name] = getExtension(name);
|
| | 114 | end
|
| | 115 |
|
| | 116 | % convert back to char if input was char
|
| | 117 | if ~fileIsString
|
| | 118 | if numOutArgs > 1
|
| | 119 | [pathstr, name, ext] = returnCharOrString(pathstr, name, ext, numOutArgs);
|
| | 120 | else
|
| | 121 | pathstr = returnCharOrString(pathstr, [], [], numOutArgs);
|
| | 122 | end
|
| | 123 | end
|
< 0.001 | 8 | 124 | end
|
< 0.001 | 8 | 125 | end
|
Other subfunctions in this file are not included in this listing.