Cell array concatenation rules

1 view (last 30 days)
Jim Hokanson
Jim Hokanson on 31 May 2013
I feel a bit silly asking this, but could someone explain the reasoning or thoughts behind the concatenation of cell arrays with other Matlab data types.
I was a bit surprised to find out that concatenation of non-cell arrays with a cell array adds those elements to a cell array, but only if they are not empty. All empty values are removed.
a = {'a'} b = [a 'b'] => {'a' 'b'} c = ['a' ''] => {'a'} d = ['a' '' 'b'] = {'a' 'b'}
This is similar to what is observed for an array. In the array case this makes sense since the array is unable to handle an empty element, unlike a cell array.
For both c and d I would have expected empty elements i.e. c => {'a' ''}
An alternative is to do: e = {a{:} ''}, which yields {'a' ''}
Matlab will warn against this and suggest the proper way of doing this, which is: f = [a {''}] => {'a' ''}
So, is the concatenation behavior meant to follow the behavior of arrays where empty elements are dropped?
Any insight into this would be appreciated. It almost seems better to me if the concatenation of element with a cell array were an error, rather than have this unexpected behavior. Perhaps it is expected and obvious to most?
  1 Comment
Walter Roberson
Walter Roberson on 31 May 2013
I don't think I ever found the behavior documented. It is always a bit of s surprise to me when it works.

Sign in to comment.

Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!