How to get the name of the parent folder of a file specified by its full path?
you can try the fileparts function as follow:
[ParentFolderPath] = fileparts('C:/Data/Matlab/Dir/file.m');
[~, ParentFolderName] = fileparts(ParentFolderPath) ;
ParentFolderName = 'Dir'
Try:
parts = strsplit(path, '/');
DirPart = parts{end-1};