Removing characters from/breaking up file names

27 ビュー (過去 30 日間)
B.M.
B.M. 2014 年 2 月 16 日
コメント済み: Image Analyst 2014 年 2 月 16 日
Hello all,
I have a list of file names from which I'd like to remove some characters. Ideally, I'd like to break up the file name into parts, but can't figure out how to do that (aside from fileparts, which was useful in removing the extension). Here's an example:
02-Feb-2009_1.xls ------------------------------------- 02-Feb-2009_1
02-Feb-2009_2.xls ----after file parts ext removal----> 02-Feb-2009_2
02-Feb-2009_3.xls ------------------------------------- 02-Feb-2009_3
I would like to separate the names into dates and trial numbers (in different columns):
02-Feb-2009_1 -------------------------------------- 02-Feb-2009 / / 1
02-Feb-2009_2 --------------?--?--?----------------> 02-Feb-2009 / / 2
02-Feb-2009_3 -------------------------------------- 02-Feb-2009 / / 3
I have tried taking the length of the names, then subtracting 2 or 3, but I can't figure out how to apply that new length to each cell in the file.

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 16 日
編集済み: Mischa Kim 2014 年 2 月 16 日
B.M. you could use the strfind command:
str = '02-Feb-2009_1.xls';
str_trim = str(1:strfind(str,'.')-1)
str_trim =
02-Feb-2009_1
or, for the latter part
str = '02-Feb-2009_1';
str_1 = str(1:strfind(str,'_')-1)
str_1 =
02-Feb-2009
str_2 = str(strfind(str,'_')+1:end)
str_2 =
1
  1 件のコメント
B.M.
B.M. 2014 年 2 月 16 日
Wonderful, thanks Mischa!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 2 月 16 日
What about using the fileparts() function?
  2 件のコメント
B.M.
B.M. 2014 年 2 月 16 日
I did, to remove the .xls extension--that was easy. It was harder to clip off extra parts of the filenames (like, in my case, the underscore plus trial number). In other words, I wasn't sure how to remove a custom part of a file name rather than the pre-specified outputs in "fileparts."
Image Analyst
Image Analyst 2014 年 2 月 16 日
Sorry - I didn't read closely enough. I guess you need to do custom parsing like Mischa showed. I often do that too.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB Report Generator についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by