how to extract double value from a string?
26 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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'))
out = str2double(replace(regexp(str, '\d+,\d+', 'match'),',','.'))
更多回答(1 个)
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
2023-2-17
s='D:\MATLAB\noise_check\bilder\Image_230217_1227_Temp_ 42,75.png'
numS=s(end-8:end-4)
numS2D=replace(numS,',','.'); % may be you mean this
numD=str2double(numS2D)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!