Select last numeric character(s) of the string

44 ビュー (過去 30 日間)
Aleksandra Budzinska
Aleksandra Budzinska 2023 年 4 月 25 日
コメント済み: Dyuman Joshi 2023 年 4 月 26 日
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 件のコメント
Aleksandra Budzinska
Aleksandra Budzinska 2023 年 4 月 25 日
The example strings are: 'Sucrose low10','Sucrose high-100','Erythritol high5','Erythritol low-10', 'Control0'

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2023 年 4 月 25 日
編集済み: Stephen23 2023 年 4 月 25 日
"How can I extract the last numbers occurring at the end of different strings?"
X = "sucrose-10";
Y = str2double(regexp(X,'[-+]?\d{1,3}$','match','once'))
Y = -10
This replaces your innermost IF-ELSEIF-ELSE-END. You could probably use it to replace you entire FOR loop too.
It detects from 1 to 3 digits at the end of the text.
It will also detect an optional +/- sign in front of those digits.
See also:
  2 件のコメント
Stephen23
Stephen23 2023 年 4 月 25 日
編集済み: Stephen23 2023 年 4 月 25 日
Assuming that LOG is a table (you did not explain this in your question):
log = array2table(["scoreABC-23";"DEF 100";"scoreGHI-100";"JKLscore: 99"], 'VariableNames',{'Code'})
log = 4×1 table
Code ______________ "scoreABC-23" "DEF 100" "scoreGHI-100" "JKLscore: 99"
idx = contains(log.Code,'score','IgnoreCase',true);
log.Rating = nan(size(log.Code));
log.Rating(idx) = str2double(regexp(log.Code(idx),'[-+]?\d{1,3}$','match','once'))
log = 4×2 table
Code Rating ______________ ______ "scoreABC-23" -23 "DEF 100" NaN "scoreGHI-100" -100 "JKLscore: 99" 99
Don't fight MATLAB with loops and IFs. Think in terms of vectors and matrices.
Aleksandra Budzinska
Aleksandra Budzinska 2023 年 4 月 25 日
Dear @Stephen23 the first sugegstion worked very well and indeed I got rid of the IF-ELSEIF-ELSE-END parts. Thank you very much!!

サインインしてコメントする。

その他の回答 (1 件)

Kevin Holly
Kevin Holly 2023 年 4 月 25 日
output = 'sucrose: 10';
numbers = str2double(extract(output, digitsPattern))
numbers = 10
output = "sucrose10";
numbers = str2double(extract(output, digitsPattern))
numbers = 10
  2 件のコメント
Aleksandra Budzinska
Aleksandra Budzinska 2023 年 4 月 25 日
Dear Kevin,
Thanks for your quick reaction although I don't understand the difference between your first and second code.
I was trying to use for all the possible lengths of the digits we might have but it doesn't work properly:
if strcmp(scorestring(end-3:end),'-100')
log.rating(l) = str2double(extract(scorestring(end-3:end)));
elseif ~contains(scorestring(end-2:end))
log.rating(l) = str2double(extract(scorestring(end-2:end)));
else
log.rating(l) = str2double(extract(scorestring(end-1:end)));
Dyuman Joshi
Dyuman Joshi 2023 年 4 月 26 日
"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.
However, @Kevin Holly, this fails to include the negative sign for negative numbers -
output = 'Sucrose high-100';
numbers = str2double(extract(output, digitsPattern))
numbers = 100

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by