MATLAB Answers


How to find the size of a cell array

Asked by Brian
on 21 Jun 2011

I have a cell array (raw from xlsread) that contains multiple data types. Is there a way to measure the size (length and width) of this cell array or convert it to a matrix so I can use the size function?



No products are associated with this question.

1 Answer

Answer by Sean de Wolski
on 21 Jun 2011
 Accepted answer

A = cell(20,11);
ans =
  20    11

works for me... ? Or do you want the size of the contents of each cell?

cellsz = cellfun(@size,A,'uni',false);

Addendum per comment:

I still am not getting what you want.

clear A
>> A{1} = single(ones(4));
>> A{2} = uint8(toeplitz(1:10));
>> A{3} = 'Hello World'
A = 
  [4x4 single]    [10x10 uint8]    'Hello World'
>> size(A)
ans =
   1     3
>> cellsz = cellfun(@size,A,'uni',false);
>> cellsz{:}
ans =
   4     4
ans =
  10    10
ans =
   1    11


Matt Tearle
on 21 Jun 2011

Shouldn't be nested, if it's the raw output from xlsread, but perhaps there are multiple of these are being read into a cell?

Brian, can you show the code that's leading to the error message you've copied above (assuming that's what "??? All contents..." is), and/or explain more what you're trying to get? The raw output from xlsread is a cell array where each cell corresponds to an Excel cell, so size(raw) should give the number of rows and columns, as Sean says.

One more doubt pls..

I have a cell array that may be empty,having one element or more than one element. how can i make decision accorind to size of cell array ?? ie. size(s) = 0 1 or size(s) = 1 1 or size(s) = 3 1 . I only need the information that how many rows are there Thnks in advance

@Labeeb, this really should be a new question. The answer is to use isempty.

doc isempty

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!