How to extract information from the filename?

조회 수: 22 (최근 30일)
SGMukherjee
SGMukherjee 2018년 5월 9일
댓글: Pawel Jastrzebski 2018년 5월 9일
I have almost 6000 files with names like Arenosillo.2005.344.13.49.G13.txt where 2005 is the year, 344 is the number of day of that year and 13:49 is the time. I want to extract all these information from the filename. Please help me.

답변 (2개)

jonas
jonas 2018년 5월 9일
If you know something about the structure, then this is quite simple. Let's say the structure is [year.day.hour.min], with some arbitrary string before.
Use regexp to find the separate digits:
string='Arenosillo.2005.344.13.49.G13.txt';
[ind1,ind2]=regexp(string,'\d+')
This gives you the index at start and end of all digits, respectively. Then extract that information:
y=string(a(1):b(1))
d=string(a(2):b(2))
h=string(a(3):b(3))
m=string(a(4):b(4))
It's a bit more complicated if you have other numbers in your string, but can still be solved with regexp

Pawel Jastrzebski
Pawel Jastrzebski 2018년 5월 9일
편집: Pawel Jastrzebski 2018년 5월 9일
This will get you the structure of all of the text files in your current folder:
x = dir('*.txt') % structure
In my case it's:
x =
6×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
And this will extract all of the text file names from the structure to the cell:
y = {x.name}
Once you've got this far, it should be easy to extract the name of the file i.e with the FOR loop and break it down to the information that you need.
  댓글 수: 2
SGMukherjee
SGMukherjee 2018년 5월 9일
I already got this. list=dir('*.txt'); for n=1:length(list); filename=list(n).name; end But I want to extract the year, day number and time from the filename.
Pawel Jastrzebski
Pawel Jastrzebski 2018년 5월 9일
Try strsplit :
>> s = 'Arenosillo.2005.344.13.49.G13.txt'
s =
'Arenosillo.2005.344.13.49.G13.txt'
>> c = strsplit(s,'.')
c =
1×7 cell array
Columns 1 through 6
{'Arenosillo'} {'2005'} {'344'} {'13'} {'49'} {'G13'}
Column 7
{'txt'}

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by