Programming

Organizing Data in Cell Arrays

Cell arrays are useful for organizing data that consists of different sizes or kinds of data. Cell arrays are better than structures for applications where

• You need to access multiple fields of data with one statement.
• You want to access subsets of the data as comma-separated variable lists.
• You don't have a fixed set of field names.
• You routinely remove fields from the structure.

As an example of accessing multiple fields with one statement, assume that your data consists of

• A 3-by-4 array consisting of measurements taken for an experiment.
• A 15-character string containing a technician's name.
• A 3-by-4-by-5 array containing a record of measurements taken for the past five experiments.

For many applications, the best data construct for this data is a structure. However, if you routinely access only the first two fields of information, then a cell array might be more convenient for indexing purposes.

This example shows how to access the first and second elements of the cell array `TEST`:

• ```[newdata,name] = deal(TEST{1:2})
```

This example shows how to access the first and second elements of the structure `TEST`:

• ```newdata = TEST.measure
name = TEST.name
```

The `varargin` and `varargout` arguments are examples of the utility of cell arrays as substitutes for comma-separated lists. Create a 3-by-3 numeric array `A`:

• ```A = [0 1 2; 4 0 7; 3 1 2];
```

Now apply the `normest` (2-norm estimate) function to `A`, and assign the function output to individual cells of `B`:

• ```[B{1:2}] = normest(A)
B =
[8.8826]    [4]
```

All of the output values from the function are stored in separate cells of `B`. `B(1)` contains the norm estimate; `B(2)` contains the iteration count.

 Applying Functions and Operators Nesting Cell Arrays