MATLAB: How to find the length of string that is an object in a vector

lengthstring

I'm trying to calculate the average length of words in a sentence. I'm having trouble getting the length of a string. Here's what's happening:
>> a='string'; >> length(a)
ans =
6
That works great. But if I try it like this:
>> string='The rain in Spain.';
>>words=strsplit(string)
words =
'The' 'rain' 'in' 'Spain.'
>> words(1)
ans =
'The'
>> length(words(1))
ans =
1
It's not giving me 3, which should be the length of the first object in words.

Best Answer

Words is a cell array of strings. The statement
Words(1)
will give you the first cell, it is a cell array of strings with only one element, hence its length of 1. However, you want the content of the first cell. You use curly braces for that:
Words{1}
This returns you want to know the length of.