Search code examples
matlabfunctioncharcountingdigits

function for counting digits and char in string in Matlab R2015


I have a string inside a cell, for example '5a5ac66'. I want to count the number of digits and chars in that string.

'5a5ac66'= 4 digits (5566) and 3 chars (aac)

How can I do that? Is there any function in MATLAB?


Solution

  • A simple (not necessarily optimal) solution is the following:

    digits = sum(x >= '0' & x <= '9');
    chars = sum((x >= 'A' & x <= 'Z') | (x >= 'a' & x <= 'z'));