Skip to main content

8.2) Manipulating Cell Arrays


Subsetting cell arrays is conceptually difficult, so let's develop the example from the previous page:

[matlab]
>> last=a{2,3}
[/matlab]


[matlab]
>> c=a(1,:)
c= 'one' 'two' 'three'
[/matlab]


[matlab]
>> whos c
Name Size Bytes Class Attributes
c 1x3 358 cell
[/matlab]

  • If a cell array is all of the same type, then the function "cell2mat" can convert it into a matrix. For example:
  • [matlab]
    >> d=cell2mat(c)
    d= 'onetwothree'
    [/matlab]

  • Because the array elements were of different sizes, string "d" has been merged. A way around this is to use sprintf:
  • [matlab]
    >> s=sprintf('%s\n', c{:})
    [/matlab]

    Here we have used the colon operator to compress "c" to a vector and sprintf to extract the string elements.