Jump to content

MATLAB Programming/Arrays/Cell Arrays

From Wikibooks, open books for an open world
(Redirected from MATLAB Programming/Cell Arrays)

Cell Array Introduction

[edit | edit source]

Cell Arrays can contain differing information in every element. These types of arrays are useful when interacting with spreadsheet software.

Creating Cell Arrays

[edit | edit source]

Cell arrays follow the same conventions as regular arrays except instead of square brackets use curly brackets.

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

Cell arrays have fewer limitations than regular arrays. The regular array can hold strings; however, the string in each element must be the same length. If one element in a regular array is a string then 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 fewer limitations for the content of a cell array comes complications. While cell arrays are a powerful tool, these arrays work differently because each element can be almost anything.

Dynamic Resizing

[edit | edit source]

Cell arrays can be dynamically resized, which is a key feature in more advanced data structures. For example, a queue data structure using the commands:

 cell_array{end+1}='a';
 cell_array{end+1}='b';

An element can be popped from the front of the queue using the commands:

 cell_array(1)=[]; % remove first element - resize
 cell_array(1)=[]; % remove first element - resize

GUI Tables

[edit | edit source]

Cell arrays are used when displaying a table to a figure.

 uitable('Data',{'hello',1;2,'there'})


Converting to and from cell arrays

[edit | edit source]

Converting From a Numeric Array into a Cell Array

[edit | edit source]

Use num2cell to convert from a numeric into a cell array.

 >> cell_array = num2cell(numeric_array);


Converting From a Cell Array into a Numeric Array

[edit | edit source]

Use cell2mat to convert from a cell into a numeric array.

 >> numeric_array = cell2mat(numeric_cell_array);

External Links

[edit | edit source]

ControlTheoryPro.com