MATLAB: Take part from string

hello, how could I take part from string for ex. I have this path : C:\Users\khs\Desktop\pics\photos and want matlab to make it C:\Users\khs\Desktop\pics\ futhermore want it to take the name of folder ( khs ) please help me thanks

Best Answer

S = 'C:\Users\khs\Desktop\pics\photos';
[PathName, Folder] = fileparts(S);
Or more flexible:
S = 'C:\Users\khs\Desktop\pics\photos';
C = strsplit(S, '\');
PathName = sprintf('%s\\', C{1:end-1});
PathName(end) = [];
Folder = C{end};