How to extract a specific 3 digit number from a filename?
9 views (last 30 days)
Show older comments
I have this filename (hst_05773_05_wfpc2_f502n_wf_sci.tif) and i would like to extract the three digit number after the _f . i.e. 502, but i am not sure how to do that as i am a beginner in MATLAB Thanks in advance
0 Comments
Accepted Answer
Azzi Abdelmalek
on 1 Sep 2016
Edited: Azzi Abdelmalek
on 1 Sep 2016
s='hst_05773_05_wfpc2_f502n_wf_sci.tif'
out=regexp(s,'(?<=_f)\d{3}','match','once')
or
s='hst_05773_05_wfpc2_f502n_wf_sci.tif'
idx=strfind(s,'_f')
out=s(idx+2:idx+4)
2 Comments
Walter Roberson
on 25 Jan 2018
To detect the .02:
has_02 = isempty( strfind(filename, '.02') );
You are not clears as to what number you want to extract. I suspect you want the number before the '.log' part:
out = regexp(filename, '\d+(?=\.log)', 'match', 'once')
you could str2double() that if you wanted.
More Answers (1)
Kim Arnold
on 5 Feb 2020
Hi everybody im quite new to Matlab.
How is it if you have like 1000 files. Do you first convert them to char characters? when i do it with one file with the name '20191108_1_blank_BA_pos.mzXML' as follows:
ix=strfind(Spos.Files_names(1),'_BA');
net=char(Spos.Files_names(1))
let=net(ix-3:ix-1)
it gives me the 3 letters before _BA, here "ank".I want to find the 3 letters before _BA for all the files. The code above allows me to do this for one file. Do i have to do a for loop and is there another ways instead of converting the files to type char?`
Thanks a lot!
2 Comments
Kim Arnold
on 7 Feb 2020
Dear Walter,
Many thanks for your answer, this is much more effective than my for loop!
See Also
Categories
Find more on Live Scripts and Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!