1

How to display each individual word of a string in MATLAB version R2012a? The function strsplit doesn't work in this version. For example 'Hello, here I am'. I want to display every word on a single line.

2

2 Answers 2

3

Each word in a single line means replacing each blank with a new line:

strrep(s,' ',sprintf('\n'))
2

You can use regexp with the 'split' option:

>> str = 'Hello, here I am';
>> words = regexp(str, '\s+', 'split').'

words = 

    'Hello,'
    'here'
    'I'
    'am'

Change '\s+' to a more elaborate pattern if needed.

2
  • Thank you! And what if I just want to show a part of the string? For example just the words 'hello' and 'here'? Feb 9, 2014 at 19:13
  • 1
    words(1:2) for first to second word.
    – Daniel
    Feb 9, 2014 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.