MATLAB: Does FILEPARTS interpret UNIX hidden files as an extension with no basename

currentdirectoryMATLABparent

I would like FILEPARTS to interpret UNIX hidden files such as .bashrc as a basename with no extension, rather than the default extension with no basename.

Best Answer

FILEPARTS is intended primarily for use following checks for attributes, such as FILEATTRIB. For example:
[stat,mess] = fileattrib(filename);
if mess.directory
% Process as directory
else
% Process as file using FILEPARTS to determine extension
[pathstr, name, ext, versn] = fileparts(filename);
end
In the case of hidden UNIX files, FILEPARTS has been designed to give the same results as common filename-splitting functions in other languages. In particular, the Microsoft Visual C/C++ function _splitpath() interprets .bashrc as a file with null basename and extension '.bashrc'.
To reinterpret this as a basename with no extension, you can check for null basenames:
[pathstr, name, ext, versn] = fileparts(filename);
if isempty(name) && ~isempty(ext)
name = ext;
ext = '';
end
The behavior on hidden directories is consistent with the overall intent. File a.b.c should report basename a.b and extension .c; the rules to give this behavior also split parent directory '..' as name '.' and extension '.'.