how to separate file name and put the number at the end to count files

2 ビュー (過去 30 日間)
Nopparat
Nopparat 2012 年 8 月 28 日
I want to write a script for counting files. For example, my file name is "transport 052412_#1 3D gfp_z000_ch00.tif". I want to do 2 things. Firstly, I want to separate this name into base_name,transport 052412, and,varied_name,#1 3D gfp_z000_ch00. Then, I want to put the number at the of the file name to count such as 001, 002, and so on.
From this objective, I used fileparts [pathstr,name,ext,versn] = fileparts('filename') to separate the name of the file but it didn't work. So could you give me some advice for this.
Thanks, Nopparat
  3 件のコメント
Nopparat
Nopparat 2012 年 8 月 29 日
I don't think so. Actually, it's flexible. The key thing is to separate the name.
Walter Roberson
Walter Roberson 2012 年 8 月 29 日
Then is the rule that the underscore is the separator?
In order to separate the name, we need rules about where the name part stops.

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

採用された回答

Image Analyst
Image Analyst 2012 年 8 月 29 日
Try this:
s = 'transport 052412_#1 3D gfp_z000_ch00.tif'
[folder baseFileName extension] = fileparts(s)
underlineLocations = find(baseFileName == '_')
part1 = baseFileName(1:underlineLocations(1)-1)
part2 = baseFileName(underlineLocations(1)+1:end)
but the sentence " Then, I want to put the number at the of the file name to count such as 001, 002, and so on." does not make sense. Do you want an output like 'transport 052412_#1 3D gfp_z000_ch00_001.tif'? You'd need to give one or two examples of an output filename so we know how to build it up from rearranging the various parts.
Maybe you mean this:
s = 'transport 052412_#1 3D gfp_z000_ch00.tif'
[folder baseFileName extension] = fileparts(s)
underlineLocations = find(baseFileName == '_')
part1 = baseFileName(1:underlineLocations(1)-1)
part2 = baseFileName(underlineLocations(1)+1:end-2)
for k = 1 : 5
s = sprintf('%s_%s%2.2d.tif', part1, part2, k)
end
which gives this:
s =
transport 052412_#1 3D gfp_z000_ch00.tif
folder =
''
baseFileName =
transport 052412_#1 3D gfp_z000_ch00
extension =
.tif
underlineLocations =
17 27 32
part1 =
transport 052412
part2 =
#1 3D gfp_z000_ch
s =
transport 052412_#1 3D gfp_z000_ch01.tif
s =
transport 052412_#1 3D gfp_z000_ch02.tif
s =
transport 052412_#1 3D gfp_z000_ch03.tif
s =
transport 052412_#1 3D gfp_z000_ch04.tif
s =
transport 052412_#1 3D gfp_z000_ch05.tif
  1 件のコメント
Nopparat
Nopparat 2012 年 8 月 31 日
Thank you very much for your information!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by