MATLAB Cell Arrays


Revision as of 16:27, 15 April 2013 by Gabe Spradlin (Talk | contribs) (Created page with "{{Header|MATLAB Arrays|:Category:MATLAB||Arrays in MATLAB|All MATLAB Articles}} Category:MATLAB ==Introduction== Cell arrays are created just like regular arrays except ...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
MATLAB Cell Arrays
Green carrot left.gif
Arrays in MATLAB All MATLAB Articles
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.

1 Introduction

Cell arrays are created just like regular arrays except that curly brackets are used instead of square brackets.

 array = [1, 2, 3; 4, 5, 6];
 cell_array = {1, 2, 3; 4, 5, 6};

The array variable is 1 array with 2 rows and 3 columns. Each element is a scalar. The cell_array variable is essentially an array or arrays. In this case the cell_array variable is made up of 6 arrays with 1 scalar element in each of the 6 arrays.

Cell arrays have fewer limitations than regular arrays. The regular array, defined by the square brackets, can hold numbers or strings but if it holds strings in each element all the strings must be the same length. Also, if 1 element of an array is a string all elements must be a string. Cell arrays have neither of these limitations.

 >> cell_array = {1, 2, 'a', 'abc'; rand(3, 2), magic(3), eye(3), 'junk'}
        cell_array = 
          [         1]    [         2]    'a'             'abc' 
          [3x2 double]    [3x3 double]    [3x3 double]    'junk' 

With the lack of limitations/rules for the content of a cell array comes complications. These cell arrays are very powerful tools but take a lot of time to get used to because each element can be almost anything.

1.1 Usage

Cell arrays are the ideal way to store data of different types. They are especially useful for storing data to be used with xlswrite. Additionally, the cell arrays are used to name the inputs and outputs of State-Space objects. Typically, this author (Gabe 00:22, 19 April 2008 (CDT)), uses cell arrays to store search strings of different lengths, file names, etc. where the data needs to be captured but there aren't many rules about the length or content of the data.

2 Searching Cell Arrays

Cell arrays can be searched using the strmatch function provided by Mathworks. The usage is as follows

 >> a = {'a', 'b', 'c', 'd', 'e', 'f'};
 >> strmatch('b', a)
         ans =

The strmatch function only works with strings not numbers.

 >> a = {'a', 'b', 'c', 'd', 1, 'f'};
 >> strmatch('b', 1)                               % The 1 is a number not a string
         ans =

3 Applying functions to each cell in the cell array

In certain cases it is useful to apply functions to each element in a cell array. This can be done with the cellfun function.

 >> a = {'a', 'b', 'c', 'd', 'e', 'f'};
 >> b = cellfun(@upper, a)
         b =
 >> b = cellfun('length', a)
         b =
           1     1     1     1     1     1

Below is a table of functions supported for this last call to cellfun.

FunctionReturn Value
isemptytrue for an empty cell element
islogicaltrue for a logical cell element
isrealtrue for a real cell element
lengthLength of the cell element
ndimsNumber of dimensions of the cell element
prodofsize Number of elements in the cell element