Search code examples
matlabmatrixcell-array

Convert cell arrays of cell array to list in matlab


I have a cell array of cell arrays of strings in matlab. I want to convert this to a simple list of unique strings so that I can access a string by its index in the list. What is the fastest way to do this? Example -

C = {1x3 cell}    {1x2 cell}
>> C{1}
ans = 'What's'    'up'    'man'
>> C{2}
ans = 'What's'    'there'

And I want a list of size 4 such that each index refers to a unique word - 'What's', 'up', 'man', 'there'. Not sure if this list should be cell array or matrix or what for it to be most efficient.


Solution

  • Here is the code to do what you need. You can use it for any size of array.

    Cunq = unique(horzcat(C{:}),'stable');