Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cell Arrays: An Introduction Without an understanding of how cell arrays work and how to interact with them, cell arrays can be one of the most frustrating.

Similar presentations


Presentation on theme: "Cell Arrays: An Introduction Without an understanding of how cell arrays work and how to interact with them, cell arrays can be one of the most frustrating."— Presentation transcript:

1 Cell Arrays: An Introduction Without an understanding of how cell arrays work and how to interact with them, cell arrays can be one of the most frustrating aspects of Matlab. They are used by a lot of built in functions (ie – textscan, textread) and are particularly useful within your own scripts. Unfortunately, many of the tricks you’ve learned for interacting with typed arrays (double, integer, and character arrays) won’t work with cell arrays.

2 What is a cell array? A cell array is one of the Matlab’s basic data structures. The one you are already quite familiar with is the singularly typed array. A typed array (ie – matrix of doubles, character array) consists entirely of a set 1 type of elements (double, character). As an analogy, think of a typed array as a box of wine bottles. Every bottle in the box is exactly the same and has the same properties. Each bottle can be opened the same way. Hence, opening the wine bottles can be automated without conditional logic (“if this is a wine bottle, do this. If it isn’t a wine bottle, do something else”). Cell arrays, on the other hand, would be like a box holding another box of 2 wine bottles, a box of 3 beer bottles, a banana and a magazine. Now, since these objects have very little, if any, properties in common (they are not the same type) we cannot automate how we interact with these objects without conditional logic. A cell array, therefore, is an data structure that provides a means for organizing dissimilar objects. In matlab, these can be strings with different lengths, doubles, other cell arrays, structures, class instantiations, graphics handles, etc.

3 Cell indexing Cell arrays, confusingly, have two ways of letting you manipulate their contents. The first is cell indexing. Cell indexing allows you to access and manipulate the cells themselves. When accessing the cells themselves, you ignore the content of the cells and merely manipulate the cells. To continue with our box of bottle boxes, literature and food cell array example, you would use cell indexing to move just the wine bottle box (assuming you knew their locations) to a new box or a different location. Generally cell indexing is used only to select chunks of a larger array or to reassign subsets of the cell array (ie - replace the wine bottle box with a box of rum bottles). Cell indexing is performed using the parenthesis operator (): –C(1,:) –C(14) –C(end)

4 Content indexing The second type of indexing is content indexing. Content indexing allows you to access and manipulate the contents of the cells. To continue with our box of bottle boxes, literature and food cell array example, you would use content indexing to open a bottle and drink the wine. Obviously, then, content indexing is more important and is used much more often. If content indexing allows you to get at the contents of the cell, what happens if the cell holds an array, can you get at the contents of that array too? Yes! For example: >> c = { [1 2 1 2 -99 2 2 2 3] ; … ‘This is a string’} c = [1x9 double] ‘This is a string’ >>c{2}(1:4) ans = This

5 Why we hate cell arrays Vectorizing does not work for cell arrays! The main feature of matlab! The beauty of having a matrix as your base data type! It all is wasted on cell arrays because you can’t work on them in any useful parallel way. But there’s hope: learning to let go. Just realize that like people in society, cell arrays and typed arrays both have intrinsic worth and are pretty good at what they do, you just can’t expect one to be very good at the other’s job. When you are working with lots of numbers, cell arrays are horrible. Use a double array, because the reason you are using matlab is for its wonderful ability to do things in parallel, right?

6 Why we hate cell arrays: An example We’ll set c equal to a cell array containing a vector of doubles: >> c = {[1 2 1 1 2 -99 2 3 -99 2]} c = [1x10 double] Now, we try to set all -99’s in c equal to NaNs: >> c{c == -99} = NaN ??? Function 'eq' is not defined for values of class 'cell'. Which we can’t do, because for all matlab knows, c could contain a string or another cell array, and what does it mean for a cell array to be equal to -99? But if we do the same thing using an array of doubles, it’s easy as pie: >> c = [1 2 1 1 2 -99 2 3 -99 2] c = 1 2 1 1 2 -99 2 3 -99 2 >> c(c == -99) = NaN c = 1 2 1 1 2 NaN 2 3 NaN 2

7 Well then, when ARE they useful? Cell arrays really shine when you need to hold a lot of varying types of information about something in one easy to manage, indexed variable. Another data type, the structure, is also useful for this sort of thing, but that’s a topic for another date. Think then, as an example, of a script that performs a calibration using a least squares fit. You can use a cell array to hold: –The x and y data vectors (m x 1 arrays of doubles) –The R Squared value (scalar double) –The coefficients of the fit (a,b for y=ax + b – both scalar doubles) –The model used (‘y = ax + b’, a string) –A graph of the data plus the fit (a graphics handle of the plot) Now, instead of having 7 different variables to keep track of, you have 1. Another very common use for cell arrays is for data input…

8 Data Input If you need to input mixed data (numbers and letters) into matlab you have 3 choices: textscan, textread, and low-level functions like fgetl. The low-level functions don’t require the use of cell arrays, but they are a pain to deal with, so you’ll want to use textread or textscan. Guess what? Both of ‘em use cell arrays to hold their data. This makes it easy to input large numbers of text and number fields without having to hold them in different typed arrays. It also means you can have an array of strings of varying lengths (no one likes to pad with spaces…) As an example, say you had a textfile with the following data: Amy, 15, 0.2525 Carl, 17, 2.5646 Simon, 2, 963.395 The command c = textscan(fopen('blah.txt','rt'),'%s%f%f','delimiter',',') will spit out a cell array with 3 cells, each containing a cell array which holds each column in the above data set. Now if we want to print out a list of all the people involved with this data set, we just type disp(c{1}).

9 Some FUNdamentals… Creating a cell array, part 1 (the one stop quickie way): >> c = { ‘This is one cell’, [1 2 25 2 5 2], 6} c = 'This is one cell' [1x6 double] [1] Creating a cell array, part 2 (the long way, typically you would use this in conjunction with a for loop, when you don’t know ahead of time what will be in your array): >> c = cell(1,3); >>c{1} = ‘This is one cell’; >>c{2} = [1 2 25 2 5 2]; >>c{3} = 1; >>c c = 'This is one cell' [1x6 double] [1] Deleting a cell, or many cells: >>c(1:2) = [ ]; >>c(1) ans = [1] >>c(2) ??? Index exceeds matrix dimensions.

10 Some more FUNdamentals… Creating nested cell arrays, when you just can’t get enough of the things: >>c = { [1 2 5 25], {‘This is a Cell array’},2} c = [1x4 double] {1x1 cell} [1] >>c{2}{1}(1:4) ans = ‘This’ Creating numeric arrays from numeric arrays stored in cells: >>c = {[1 2 3 4],[2 4 6 8]}; >>x = [c{1};c{2}] x = 1 2 3 4 2 4 6 8 To convert from a cell array to a character array (when applicable) use char To convert back to a cell array from a char array, use cellstr To convert from a double array to a cell, use num2cell To apply certain functions to a cell array (as long as they make sense) use cellfun

11 Cell arrays: The array of tomorrow, today! Don’t be afraid to use cell arrays. They make life easier.


Download ppt "Cell Arrays: An Introduction Without an understanding of how cell arrays work and how to interact with them, cell arrays can be one of the most frustrating."

Similar presentations


Ads by Google