How to extract a specific 3 digit number from a filename?

조회 수: 11 (최근 30일)
Mohammed
Mohammed 2016년 9월 1일
댓글: Kim Arnold 2020년 2월 7일
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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 9월 1일
편집: Azzi Abdelmalek 2016년 9월 1일
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
Songman Liao
Songman Liao 2018년 1월 25일
Hi Azzi, I actually have 24 files, 12 of them have names starting from "AMS1701-AMsent1.log" to "AMS1701-AMsent12.log". And the rest have names like "AMS1701.02-AMsent1.log" to "AMS1701.02-AMsent12.log". There are two kinds of number I want to extract. The first type is whether "AMS1701" with ".02" or without it. The second type of number is the number following "AMsent". Can you help me with that? Thank you so much!
Walter Roberson
Walter Roberson 2018년 1월 25일
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.

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

추가 답변 (1개)

Kim Arnold
Kim Arnold 2020년 2월 5일
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
Walter Roberson
Walter Roberson 2020년 2월 5일
dinfo = dir('*XML'); %not *.XML as the extension is mzXML for some reason
filenames = {dinfo.name};
lets = regexp(filenames, '...(?=_BA)', 'match', 'once');
lets will now be a cell array of character vectors, with one entry for each XML file found in the current directory, and the entry will be the 3 characters before _BA in the file name. To emphasize, lets will have done all of them at once, so lets{1} would be the first, lets{2} would be the second, and so on.
Kim Arnold
Kim Arnold 2020년 2월 7일
Dear Walter,
Many thanks for your answer, this is much more effective than my for loop!

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

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by