1

Hi can any one help me in dealing with strings in MATLAB. For example, the string

A = 'A good looking boy'

how can we store these individual words in arrays and later retrieve any of the words?

1

3 Answers 3

2

As found here, you could use

>> A = 'A good looking boy';
>> C = regexp(A,'[A-z]*', 'match')
C = 
    'A'    'good'    'looking'    'boy'

so that

>> C{1}
ans = 
    A

>> C{4}
ans = 
    boy

>> [C{:}]
ans =
    Agoodlookingboy
Sign up to request clarification or add additional context in comments.

Comments

1

The most intuitive way would be using strsplit

C = strsplit(A,' ')

However as it is not available in my version I suppose this is only a builtin function in matlab 2013a and above. You can find the documentation here.

If you are using an older version of matlab, you can also choose to get this File Exchange solution, which basically does the same.

Comments

1

You can use the simple function textscan for that:

C = textscan(A,'%s');

C will be a cell array. This function is in Matlab at least since R14.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.