Remove path from file name

조회 수: 31 (최근 30일)
Douglas Anderson
Douglas Anderson 2014년 12월 11일
댓글: Chad Greene 2014년 12월 11일
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')
Chad Greene
Chad Greene 2014년 12월 11일
Ah, very cool!

댓글을 달려면 로그인하십시오.

추가 답변 (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에서 String Parsing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by