MATLAB: Merging cell arrays with variable number of elements

merging cell arrays

I have a cell array x that can be of two forms depending on some condition cond
if cond x = {A B1 B2 C} else x = {A B1 B2 B3 C} end
where A, B1, B2, B3, and C are all row vectors. I want to define a variable B that gets set based on cond, and then have a single expression for x:
if cond B = {B1 B2} else B = {B1 B2 B3} end x= {A B C}
The above does not work because it will treat B as a cell array, not a group of vectors. For instance, with cond = 0, we get something like the following type for x
[1x2 double] {1x3 cell} [1x2 double]
How could I make the result to look like
[1x2 double] [1x3 double] [1x5 double] [1x2 double] [1x2 double]
Thanks Farzad

Best Answer

x = {A, B{:}, C} ;
In this expression, B{:} is a comma separated list (CSL); it is equivalent to listing B's cells' content separated by commas.