How can I extract all numbers from a cell array of strings?
1 view (last 30 days)
Show older comments
MathWorks Support Team
on 4 Dec 2009
Commented: Jiri Hajek
on 23 Oct 2020
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
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
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).
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown 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!