Remove path from file name

31 views (last 30 days)
Douglas Anderson
Douglas Anderson on 11 Dec 2014
Commented: Chad Greene on 11 Dec 2014
Hello!
There's probably a simple tool for this, but I can't find it:
Have a string for a file name that includes the full path, like:
C:\Users\DougAnderson\Documents\MATLAB\SHtest\sig hole 1.xlsx
where the last part (the actual file name) may contain spaces. I would like to change this to, for example
sig hole 1
Thanks
Doug

Accepted Answer

Chad Greene
Chad Greene on 11 Dec 2014
Edited: Chad Greene on 11 Dec 2014
You can use fileparts. I had to switch the direction of the slashes because I'm using a mac, but it should work on your machine keeping the slashes the way you wrote them:
[~,name,~] = fileparts('C:/Users/DougAnderson/Documents/MATLAB/SHtest/sig hole 1.xlsx')
name =
sig hole 1
  2 Comments
Sean de Wolski
Sean de Wolski on 11 Dec 2014
Chad, fullfile will handle filesep for you.
fullfile('C:\Documents/MATLAB')
Chad Greene
Chad Greene on 11 Dec 2014
Ah, very cool!

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 11 Dec 2014
s='C:\Users\DougAnderson\Documents\MATLAB\SHtest\sig\hole 1.xlsx'
out=regexp(s,'(?<=\\)[^\\]+$','match')
  1 Comment
Douglas Anderson
Douglas Anderson on 11 Dec 2014
Thank you, Azzi. I thought I might have to go to "regexp()" to get this done, but "fileparts()" makes my task simpler!

Sign in to comment.


Douglas Anderson
Douglas Anderson on 11 Dec 2014
Ah, Perfect!!!! Thank you, Chad.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!