MATLAB: Extracting data from an array

arraycolumnsextracting datamatrix

I have an array of 69 x 1 cells. All cells are either 12 x 2 or 13 x 2 matrices. I want to make a matrix, with all second columns from the matrices in the array.
So for example:
This is my array: (I havent given names to the cells yet, but just for the example)
[y1; y2; y3; y4; y5…….]
y1 = 12 x 2 matrix y2 = 12 x 2 matrix y3 = 13 x 2 matrix y4 = 12 x 2 matrix y5 = 13 x 2 matrix
I want all second columns from y1 till y69 in a n x 69 matrix.
Thanks for the help!

Best Answer

You're mixing up terms and notations, so it's not clear what you have and want. My understanding it that you have a cell array (size 69 x 1) consisting of matrices (size N x 2). The notation for cell arrays uses {}:
carr = {y1; y2; y3; ...} %yn can all be of different size and even type
Because the number of rows in each of these matrices is different (either 12 or 13), it's not possible to combine these seconds column into a matrix. It is however possible to combine them into a cell array. This is easily done with cellfun:
carr = {rand(12, 2); rand(13, 2); rand(12, 2); rand(13, 2)}; %demo data
column2 = cellfun(@(m) m(:, 2), carr, 'UniformOutput', false);
If cellfun is too complex for you, you can use a loop. The cellfun above is exactly equivalent to:
column2 = cell(size(carr));
for cidx = 1:numel(carr)
column2{cidx} = carr{cidx}(:, 2);
end