1

I have a cell array, where each cell is a string of the following form:

A{1} = hello, world, the, hello, the, how, are, you, world

I would like to retain only the unique words, to give me a new char array that will go into an output cell array:

B{1} = hello, world, the, how, are, you

unique() does not seem to cut it. Is there an easy way of doing this?

1
  • How have you tried to use unique? If used in the proper way it should do exactly what you need. Commented Jan 15, 2014 at 15:52

3 Answers 3

3

I agree with some previous comments on questioning why the data is in this format (if you have a choice).

However:

>> A{1} = 'hello, world, the, hello, the, how, are, you, world';
>> B{1} = strjoin(unique(strtrim(strsplit(A{1}, ','))), ', ')

B = 

    'are, hello, how, the, world, you'

Hope that helps.

* EDIT * another solution that removes strtrim if you know there will always be a space. Also, if you do not want the operation to sort the strings you can use a 'stable' call to unique:

>> B{1} = strjoin(unique(strsplit(A{1}, ', '),'stable'), ', ')

B = 

    'hello, world, the, how, are, you'
Sign up to request clarification or add additional context in comments.

2 Comments

Seems a cleaner solution than mine but I didn't remember the strsplit function. In fact I'm running 2012b so I don't have this function. strsplit is available from release 2013a.
Thanks ! I tried the strsplit() version, and it is exactly what I needed. As to answer the question as to why the data was in this format, it was given to me that way, and I do not have control over it.
2

I suspect that A{1} is one long string.

A{1} = 'hello, world, the, hello, the, how, are, you, world';

In which case unique will just return the string or characters.

>> unique(A)
ans = 
    'hello, world, the, hello, the, how, are, you, world'

>> unique(A{:})
ans =
    ,adehlortuwy.

The string needs to be converted to a cell array first.

>> a = textscan(A{1},'%s','delimiter',',')
a = 
    {9x1 cell}

>> b = unique(a{:})
b = 
    'are'
    'hello'
    'how'
    'the'
    'world'
    'you'

and then if you want to convert back to a long string again

>> sprintf('%s,',b{:})
ans =
    are,hello,how,the,world,you,

Although to be honest if I was having to process strings in this way I wouldn't start with them all in one long string.

Comments

0

You probably didn't use unique() properly. It should work well. See example:

A = {'one','two','twenty-two','One','two'};
C = unique(A)

You will get

C = 

    'One'    'one'    'twenty-two'    'two'

2 Comments

An addition to get from a sting to a cell array and back would make this complete.
@DennisJaheruddin Probably no need here. OP doesn't ask for this. :)

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.