필터 지우기
필터 지우기

how to extract double value from a string?

조회 수: 27 (최근 30일)
Happy PhD
Happy PhD 2023년 2월 17일
댓글: Luca Ferro 2023년 2월 17일
I been trying to figure our how to extract certain numbers from a string with this layout:
D:\MATLAB\noise_check\bilder\Image_230217_1227_Temp_ 42,75.png"
I want to extract the value 42.75 from it. Any ideas how I can do this?
I tried this but it gives me NaN as result:
V = str2double(regexp(fullFileNames,'\d+','match'))
This gives error "Error using sscanf, first argument mustnbe a text scalar".
d = sscanf(fullFileNames, '%d %d %d')
Thanks

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 2월 17일
이동: Star Strider 2023년 2월 17일
"I tried this but it gives me NaN as result: "
Does it? Let's check -
str = "D:\MATLAB\noise_check\bilder\Image_230217_1227_Temp_ 42,75.png";
V = str2double(regexp(str,'\d+','match'))
V = 1×4
230217 1227 42 75
out = str2double(replace(regexp(str, '\d+,\d+', 'match'),',','.'))
out = 42.7500
  댓글 수: 1
Happy PhD
Happy PhD 2023년 2월 17일
편집: Happy PhD 2023년 2월 17일
My strings are an cell array so maybe thats why it doesn't work.

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

추가 답변 (1개)

Luca Ferro
Luca Ferro 2023년 2월 17일
편집: Luca Ferro 2023년 2월 17일
assuming the string is always formatted as such you could just:
s='D:\MATLAB\noise_check\bilder\Image_230217_1227_Temp_ 42,75.png'
numS=s(end-8:end-4);
numS2D=replace(numS(',','.'); %necessary since the conversion to double needs . and not ,
numD=str2double(numS2D)
  댓글 수: 2
VBBV
VBBV 2023년 2월 17일
s='D:\MATLAB\noise_check\bilder\Image_230217_1227_Temp_ 42,75.png'
s = 'D:\MATLAB\noise_check\bilder\Image_230217_1227_Temp_ 42,75.png'
numS=s(end-8:end-4)
numS = '42,75'
numS2D=replace(numS,',','.'); % may be you mean this
numD=str2double(numS2D)
numD = 42.7500
Luca Ferro
Luca Ferro 2023년 2월 17일
yep, typo. Thanks for the correction

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by