How can I extract all numbers from a cell array of strings?

1 view (last 30 days)
I have a cell array of strings. Each string contains at maximum one number and an unknown amount of other characters, for example 'abc13def'. I would like to extract these numbers from the cell array into a numerical array. The elements of the array corresponding to cells which have no number at all should be set to zero.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Oct 2016
A function to automatically extract numerical data from a cell array of strings is not available in MATLAB 7.9 (R2009b).
The attached function, strip_cell_to_num, can be used to perform this task using regular expressions.
  1 Comment
Jiri Hajek
Jiri Hajek on 23 Oct 2020
Nice solution, it helped me as well.
An additional benefit of the 'once' option in regexp is that the result is a cell array of strings (not cell array of cells).

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!