Remove text in a string with numbers

60 views (last 30 days)
How can I remove the text part and obtain a vector of just the last numbers?

Accepted Answer

Image Analyst
Image Analyst on 20 Apr 2021
Edited: Image Analyst on 20 Apr 2021
You need to use digitsPattern:
txt = 'Model1__DK1_5450.0 '
pat = digitsPattern;
onlyNumbers = extract(txt, pat)
onlyNumbers =
4×1 cell array
{'1' }
{'1' }
{'0' }
or you can use logical indexing:
digitIndexes = txt >= '0' & txt <= '9';
onlyNumbers = txt(digitIndexes)
onlyNumbers =
Or you can do fancier parsing if you just want certain numbers. Such as:
setOf4Digits = onlyNumbers(3:end-1); % Get only the 5450
Do you want the underlines, dots, and minus signs included?
Image Analyst
Image Analyst on 21 Apr 2021
@Chiara Scarpellini, just checking back. You did not accept either answer. Are you still there?

Sign in to comment.

More Answers (0)


Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!