Select last numeric character(s) of the string
이전 댓글 표시
Hello,
I'm hoping for an advise on the code which has to do with extracting the digits (either 1,2 or 3) from the string which includes different condition names. It's about a rating from -100 to 100 given to different conditions.
In our other script the output gave the ratings saves as: e.g "sucrose: 10". Now it's: e.g. "sucrose10".
How can I extract the last numbers occurring at the end of different strings? I'll provide the code we had below to deal with the separation using ":" which now doesn't work.
log.rating = zeros(height(log),1);
for l = 1:height(log)
if contains(char(log.Code(l)),'score','IgnoreCase',true)
scorestring = char(log.Code(l));
if strcmp(scorestring(end-3:end),'-100')
log.rating(l) = str2double(str(scorestring(end-3:end)));
elseif ~contains(scorestring(end-2:end))
log.rating(l) = str2double(str(scorestring(end-2:end)));
else
log.rating(l) = str2double(str(scorestring(end-1:end)));
end
else
log.rating(l) = NaN;
end
end
Many thanks!!
채택된 답변
추가 답변 (1개)
output = 'sucrose: 10';
numbers = str2double(extract(output, digitsPattern))
output = "sucrose10";
numbers = str2double(extract(output, digitsPattern))
댓글 수: 2
Aleksandra Budzinska
2023년 4월 25일
"although I don't understand the difference between your first and second code. "
@Aleksandra Budzinska, they are the same code, Kevin just used two different inputs to show the result.
output = 'Sucrose high-100';
numbers = str2double(extract(output, digitsPattern))
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!