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?
Yes, there's a built-in function for that, isstrprop. It tells you which characters are within the given range, in your case 'digit' or 'alpha'. You can then use nnz to obtain the number of such characters:
str = {'5a5ac66'};
n_digits = nnz(isstrprop(str{1},'digit')); %// digits
n_alpha = nnz(isstrprop(str{1},'alpha')); %// alphabetic
If you have a cell array with several strings:
str = {'5a5ac66', '33er5'};
n_digits = cellfun(@nnz, isstrprop(str,'digit')); %// digits
n_alpha = cellfun(@nnz, isstrprop(str,'alpha')); %// alphabetic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With