# MATLAB: Adding continuously to get a sum of how many words there are

## MATLAB: Adding continuously to get a sum of how many words there are

matlab functionstring

``function getWordsstr = char('jeff john 2td');n = size(str,1);for t=0    for i = 1:n        if str(i,1) == isletter(str(i,1))            t+1;        end        if str(i,1) == isnumeric(str(i,1))            disp('For', str(i,1), 'it isnt a word')        end    endenddisp(t);end``
I want to continuously add +1 to my t value of originally 0 when I get another word into my string, but as of right now it keeps displaying “t” as 0 for some reason. Trying to get the function to be capable of displaying how many words there are, but if the one of the words starts with a number the function WILL NOT count it as a word. For example: if the string is ‘my 2sons had lunch’ it would display it as the t variable having 3 words.
``function wordcount = getWordsstr = 'jeff john 2lion lion2 this2that dog.';strcell = regexpi(str, 'w*', 'match');%Defining word as one that starts with a letter (this2that would still be a word)wordloc = cellfun(@(x) ~isempty(regexpi(x(1), '[a-z]')), strcell);wordcount = sum(wordloc); %number of wordsnonwordcount = sum(~wordloc); %number of non-words%Displaying words /  nonwordsfprintf('Is a word: %sn', strcell{wordloc})fprintf('Not a word: %sn', strcell{~wordloc})``
``Is a word: jeffIs a word: johnIs a word: lion2Is a word: this2thatIs a word: dogNot a word: 2lion``
``function getWordsstr = 'jeff john 2td tomorrow 3e4 a23 dog.';strcell = regexp(str, 's+', 'split');wordloc = cellfun(@(x) isempty(regexp(x, '[^a-zA-Z.]', 'once')), strcell); %gets location of string with only letter a-zA-Zwordcount = sum(wordloc); %number of words, 4nonwordcount = sum(~wordloc); %number of non-words``