Wanting to isolate a number from a string

조회 수: 4 (최근 30일)
Stephen Devlin
Stephen Devlin 2018년 6월 28일
댓글: Stephen Devlin 2018년 6월 28일
hi,
I have a struct with a list of filenames which I want to extract a piece of data from, the filename is in the format
ZYXW1_4.0kHz_1_P.tif
or
ZYXW1_26.0kHz_1_P.tif
All I wish to be left with is
4.0_1
or
(4.0,1)
So that I can write those digits onto an image.I have used strep and regex to isolate pieces of data in the past from a string but have not been able to figure out how to isolate data before an expression such as kHz etc.
I have
str=('ZYXW1_4.0kHz_1_P.tif')
expression = '\d*\.\dkHz';
matchStr = regexp(str,expression,'match')
but it won't give me the "_1".
Any ideas?

채택된 답변

Stephen23
Stephen23 2018년 6월 28일
편집: Stephen23 2018년 6월 28일
>> str = 'ZYXW1_4.0kHz_1_P.tif';
>> regexprep(str,'^.*_(\d+\.?\d*)\D+_(\d+)_\D+$','($1,$2)')
ans = (4.0,1)
  댓글 수: 1
Stephen Devlin
Stephen Devlin 2018년 6월 28일
I just came up with
str=('ZYXW1_26.0kHz_1_P.tif')
expression = '\d*\.\dkHz\w\d*';
matchStr = regexp(str,expression,'match')
text_out=strrep(matchStr,'kHz','')
But I'll accept this because its better, thank you Stephen

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

추가 답변 (0개)

카테고리

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