毎回似たような実装をしている気がしたので、ある程度きちんと実装したときのコードをメモしておく。
実装
getFilenameFromFilepath.m
function filename_string = getFilenameFromFilepath(filepath)
% getFilenameFromFilepath: Extracts the filename (including extension) from a given filepath.
%
% Input:
% - filepath: A char or string representing the full path to a file.
%
% Output:
% - filename_string: A string representing the filename extracted from the filepath.
%
% Note: The filename is first generated as a char array to allow for easy modifications
% in the future if a char type output becomes necessary.
arguments
filepath (1, :) {mustBeText} % ensures that filepath is either char or string
end
[~, name, ext] = fileparts(filepath);
filename_char = strcat(name, ext);
filename_string = string(filename_char);
end