Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 7

Task 7.5 Detail: Using cell arrays

Summary of new tools and commands.

Task: Create a general tool (script) to make a simple line plot where the two vectors and the labels (input to title, xlabel, and ylabel) are all held by one cell array. Create a simple graph using this script.

Cell arrays are one mechanism to have a single variable hold several dissimilar items (variables of different kinds). It is one of two ways in MATLAB of collecting information (structure is the other which we will see shortly).

As you recall, we talked about character arrays to hold a set of names (say, file names) but all of the names had to have the same number of characers. Cell arrays do not have this restriction on arrays of characters.

A cell array is created by simply defining the value of a variable with indexes. The indicator that a cell array is being created is the use of "curly brackets" or {} in place of parenthesis. The following commands create a cell array:

  TEST{1}=1:10;
  TEST{2}=cos(1:10);
  TEST{3}='Time';
  TEST{4}='Response';

A more useful example returns to the issue of file names with different number of characters in the names.

  FileNames{1}='FirstDataFile.dat';
  FileNames{2}='Data2.dat';
  FileNames{3}='ThirdFile.dat';

These names can now be used to load the contents of these files:

  for i=1:3
    data=load(char(FileNames{i}));
%    do work on the data ...
  end
One wrinkle with cell arrays holding character strings is that they cannot be used directly where a character string is used. They must first be converted to a character array, which is done above with the char() command.

A cell array can also be created with the cell(r,c) command which creates an empty cell array with the indicated number of rows and columns. Reference to the various elements of the array are indicated by the row and column index, but using curly brackets {} in place of parenthesis.

To solve the task here, consider the information that is needed to create a line plot. There are 5 items: the x and y arrays of numbers to plot, and three character strings containing the title, x label and y label. You can put all of these in a cell array,

With a little thought, you could add controls for the line color and style, control for the axis scales and so forth.

One problem with this scheme is that you must use the same name (PlotInfo) to pass information to the plotting script. In the next class we will learn about functions which will remove this restriction.

Flow chart to solve the task

%%%  Define the 5 items to construct the plot in a cell array
%%%  Create the plot using the array values
%%%  add the title
%%%  add the labels

Script to solve the task

%%%  Define the 5 items to construct the plot in a cell array
  PlotInfo=cell(5,1);
%     assume arrays were read from a file or created somehow
  PlotInfo{1}=x;
  PlotInfo{2}=y;
  PlotInfo{3}='Plot title';
  PlotInfo{4}='x axis label';
  PlotInfo{5}='y axis label';
%%%  Create the plot using the array values
  figure
    plot(PlotInfo{1},PlotInfo{2});
%%%  add the title
    title(char(PlotInfo{3}));
%%%  add the labels
    xlabel(char(PlotInfo{4}));
    ylabel(char(PlotInfo{5}));

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 7


email: J. Klinck