MATLAB: How to concatenate a 3D cell array along the 3rd dimension

cell arrayconversion

Dear experts, I am trying to convert CellArray1 into CellArray2, by concatenating CellArray1 along the third dimension. Any ideas on how to do that efficiently?
CellArray1 =
2×2×3 cell array
val(:,:,1) =
{180×1 double} {180×1 double}
{180×1 double} {180×1 double}
val(:,:,2) =
{180×1 double} {180×1 double}
{180×1 double} {180×1 double}
val(:,:,3) =
{180×1 double} {180×1 double}
{180×1 double} {180×1 double}
CellArray2 =
2×2 cell array
{540×1 double} {540×1 double}
{540×1 double} {540×1 double}
Thank you so much!

Best Answer

In one line using num2cell, cellfun, and vertcat:
>> C1 = cell(2,2,3); % preallocate cell array.
>> C1(:) = cellfun(@(~)rand(180,1),C1,'uni',0); % fake data.
>> C2 = cellfun(@(c)vertcat(c{:}),num2cell(C1,3),'uni',0);
And checking the first element of C2:
>> size(C2)
ans =
2 2
>> size(C2{1})
ans =
540 1
>> isequal(C2{1},vertcat(C1{1,1,:}))
ans = 1