diff --git a/Utility functions/splitFilename.m b/Utility functions/splitFilename.m new file mode 100644 index 0000000..918752a --- /dev/null +++ b/Utility functions/splitFilename.m @@ -0,0 +1,32 @@ +% Split filename into name and extension (if present), applying some more +% elaborate procedure to determine the real extension than that used +% in fileparts() + +function [name, ext] = splitFilename(filename) + filename_split = regexp(filename, '\.', 'split'); + + if length(filename_split) == 1 + + % No extension found + name = filename; + ext = ''; + return + end + + % A candidate for the extension + ext = filename_split{end}; + + if ~isempty(ext) && ~any(isspace(ext)) && length(ext)<4 + + % ext is actual extension + % Add a point to conform with the convention of fileparts() + ext = ['.' ext]; + name = strjoin(filename_split(1:end-1), '.'); + else + + % ext is not an actual extension + name = filename; + ext = ''; + end +end +