Remove path from file name

33 ビュー (過去 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 Center および File ExchangeString Parsing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by