Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 645
Ha Hacker Avatar asked Dec 04 '22 02:12

Ha Hacker


1 Answers

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
like image 151
Luis Mendo Avatar answered Dec 29 '22 07:12

Luis Mendo