1

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.

0

2 Answers 2

1

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'
Sign up to request clarification or add additional context in comments.

Comments

1

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'}

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.