How to extract information from the filename?

36 views (last 30 days)
SGMukherjee on 9 May 2018
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.

Answers (2)

jonas on 9 May 2018
If you know something about the structure, then this is quite simple. Let's say the structure is [], with some arbitrary string before.
Use regexp to find the separate digits:
This gives you the index at start and end of all digits, respectively. Then extract that information:
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 on 9 May 2018
Edited: Pawel Jastrzebski on 9 May 2018
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:
And this will extract all of the text file names from the structure to the cell:
y = {}
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.
Pawel Jastrzebski
Pawel Jastrzebski on 9 May 2018
Try strsplit :
>> s = 'Arenosillo.2005.344.13.49.G13.txt'
s =
>> c = strsplit(s,'.')
c =
1×7 cell array
Columns 1 through 6
{'Arenosillo'} {'2005'} {'344'} {'13'} {'49'} {'G13'}
Column 7

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by