MATLAB: How to scan a sentence of text, one by one word and store it in seperate memory location

textscan

How to scan sentence of text, by one by one word? that is in a sentence, while scanning a data from first character,if it recognizes a space, then the data scanned before the space needs to be stored in one variable(say A) to process further with that word. again from the left word, it needs to scan again and store the second, third, fourth, ….. upto end of the sentence in the same variable named "A".
Example: "Hello world! All is Well" is the sentence. Now it needs to scan from the word "Hello",once if it finds space after hello, it should recognizes the "Hello" as a word and again needs to start scan after space,once it finds space, it recognizes the scanned characters as a word like" world". How to per form this? kindly help on this

Best Answer

Use regexp, and adapt the regular expression to suit whatever your definition of word is. The words are all returned in the output cell array C:
>> str = 'Hello world! All is Well';
>> C = regexp(str,'\w+','match');
>> C{:}
ans = Hello
ans = world
ans = All
ans = is
ans = Well
>>
Note that there is no universal definition of what a "word" is, and beginners invariably forget about hyphens (e.g. "mother-in-law"), apostrophes (e.g. "can't"), etc.). The more specific your definition of "word" is, the more precisely you can define the regular expression to suit.