# I need to put data from columns in different cells into one vector

1 view (last 30 days)
Martin Pecha on 9 Jul 2018
Hi,
I have 1x20 cell and from each I need to take 4th column and put all of them in one long vector in order from 1-20. Moreover, these 4th vectors does not have same length. Is there any possibility for loop or any other easier solution than manually do it?
Thanks, Martin
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (2)

Robert U on 9 Jul 2018
Hi Martin Pecha,
assuming your cells contain arrays of double:
% Create input cell array
cInput = cell(1,20);
cInput = cellfun(@(cIn) rand(randi([4,10],1),randi([1,100],1)),cInput,'UniformOutput',false);
% extract 4th column and concatenate vertically
cOutput = cell2mat(cellfun(@(cIn) cIn(:,4),cInput,'UniformOutput',false)');
Kind regards,
Robert
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Pawel Jastrzebski on 9 Jul 2018
Consider the following example:
% 1. Create fake data (you'll use the real data)
% create empty cell 1x20
c = cell(1,20)
% populate the cells 1 through 20 with a matrix
% that has 4 columns and random number of rows
for i=1:size(c,2)
rcol = 4;
rrow = randi([4 10]);
c{i} = rand(rrow,rcol);
end
% 2. Extract 4th column out of every cell and
% merge into vector
vector4 = [];
for i=1:size(c,2)
% access data in i-th cell
% and extract all values (:) from row 4
GetCellData = c{i}(:,4);
% append 'GetCellData' to an existing vector
vector4 = [vector4; GetCellData];
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by