user552231
user552231

Reputation: 1135

How can I parse this string into a cell array in MATLAB?

I have a string:

sen = '0.31431 0.64431 Using drugs is not cool Speaker2';

I am trying to write code that will generate:

cell = {'0.31431','0.64431', 'Using drugs is not cool', 'Speaker2'};

The problem is that I don't want to use the number of words in 'Using drugs is not cool' because these will change in other examples.

I tried:

output = sscanf(sen,'%s %s %c %Speaker%d');  

But it doesn't work as desired.

Upvotes: 1

Views: 54

Answers (2)

matlabbit
matlabbit

Reputation: 706

You can use regexp, but it's a bit ugly:

>> str = '0.31431 0.64431 Using drugs is not cool Speaker2';
>> regexp(str,'(\d+\.\d+)\s(\d+\.\d+)\s(.*?)\s(Speaker\d+)','tokens')

ans =

  1×1 cell array

    {1×4 cell}

>> ans{:}

ans =

  1×4 cell array

    {'0.31431'}    {'0.64431'}    {'Using drugs is not cool'}    {'Speaker2'}

Upvotes: 1

gnovice
gnovice

Reputation: 125854

If you know you will always have to remove the first two words and last word, collecting everything else together, then you can use strsplit and strjoin as follows:

sen = '0.31431 0.64431 Using drugs is not cool Speaker2';
words = strsplit(sen);  % Split all words up
words = [words(1:2) {strjoin(words(3:end-1), ' ')} words(end)]  % Join words 3 to end-1

words =

  1×4 cell array

    '0.31431'    '0.64431'    'Using drugs is not cool'    'Speaker2'

Upvotes: 1

Related Questions