NEW ANSWER: treats a word as one that starts with a letter
function wordcount = getWords
str = 'jeff john 2lion lion2 this2that dog.';
strcell = regexpi(str, '\w*', 'match');
wordloc = cellfun(@(x) ~isempty(regexpi(x(1), '[a-z]')), strcell);
wordcount = sum(wordloc);
nonwordcount = sum(~wordloc);
fprintf('Is a word: %s\n', strcell{wordloc})
fprintf('Not a word: %s\n', strcell{~wordloc})
Outputs:
Is a word: jeff
Is a word: john
Is a word: lion2
Is a word: this2that
Is a word: dog
Not a word: 2lion
OLD ANSWER: One way to do this without loops:
function getWords
str = 'jeff john 2td tomorrow 3e4 a23 dog.';
strcell = regexp(str, '\s+', 'split');
wordloc = cellfun(@(x) isempty(regexp(x, '[^a-zA-Z\.]', 'once')), strcell);
wordcount = sum(wordloc);
nonwordcount = sum(~wordloc);
Best Answer