Merging cell arrays with variable number of elements

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

 Accepted Answer

Cedric
Cedric on 29 Mar 2013
Edited: Cedric on 29 Mar 2013
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.

2 Comments

Thanks Cedric, that solved the problem.
Great! Please [ Accept ] the answer if the problem is solved.

Sign in to comment.

More Answers (0)

Asked:

on 29 Mar 2013

Community Treasure Hunt

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

Start Hunting!