Remove path from file name

30 次查看(过去 30 天)
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

采纳的回答

Chad Greene
Chad Greene 2014-12-11
编辑:Chad Greene 2014-12-11
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 个评论
Sean de Wolski
Sean de Wolski 2014-12-11
Chad, fullfile will handle filesep for you.
fullfile('C:\Documents/MATLAB')

请先登录,再进行评论。

更多回答(2 个)

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

请先登录,再进行评论。


Douglas Anderson
Douglas Anderson 2014-12-11
Ah, Perfect!!!! Thank you, Chad.

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by