Clear Filters
Clear Filters

extract row from a cell

10 views (last 30 days)
Alberto Acri
Alberto Acri on 17 Jun 2023
Answered: Voss on 17 Jun 2023
Hi! I generated a cell similar to this one:
a = {'home'};
b = {'ball' , 'cake' , 'ice'};
c = {'car','moto'};
d = {'money','supercar','toys'};
e = cell(4,0);
e(1,1:numel(a)) = a;
e(2,1:numel(b)) = b;
e(3,1:numel(c)) = c;
e(4,1:numel(d)) = d
I want to extract from cell "e" the first row consisting of three values.
In the case above, the second row of cell "e" consists of three values, one per column. I want to extract this second row.

Accepted Answer

Voss
Voss on 17 Jun 2023
a = {'home'};
b = {'ball' , 'cake' , 'ice'};
c = {'car','moto'};
d = {'money','supercar','toys'};
e = cell(4,0);
e(1,1:numel(a)) = a;
e(2,1:numel(b)) = b;
e(3,1:numel(c)) = c;
e(4,1:numel(d)) = d;
% construct a logical matrix the same size as e saying whether each
% corresponding cell of e is non-empty:
e_nonempty = ~cellfun(@isempty,e)
e_nonempty = 4×3 logical array
1 0 0 1 1 1 1 1 0 1 1 1
% find the first row of e that has three non-empty cells:
row = find(sum(e_nonempty,2) == 3,1)
row = 2
% extract that row of e:
result = e(row,:)
result = 1×3 cell array
{'ball'} {'cake'} {'ice'}

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!