# Creating A Character Array

So far we have only looked at Numeric Arrays. It is also possible to construct arrays of text. When inputting text into a cell we first need to wrap the text around in ' ', for example 'MyText'. MATLAB will automatically colour Text enclosed in ' ' Purple.

TextVariable='MyText' We may open up the TextVariable in the Variable Editor we can see it is a 1 Column by 6 Rows Character Array: Note if we do not enclose the text with ' ' for example:

TextVariable=MyText

MATLAB will look for the variable MyText and throw up an error stating the Variable doesn’t exist. Next assuming we want to have a column of text, we can do so with a Character Array:

MyCharCol=['bat';'cat';'mat';'fat'] The 4 Columns by 3 Rows Character Array is created: Notice how the number of letters in each word in each cell is identical in length. If we attempt to alter the sizes for instance by addition of an e to the words mat and fat to make mate and fate respectively:

MyCharCol1=['bat';'cat';'mate';'fate'] We will get the error message:

Dimensions of arrays being concatenated are not consistent.

To rectify this we would instead need to add spaces at the end of bat and cat so each cell has the same number of characters, in this case 4:

MyCharCol1=['bat ';'cat ';'mate';'fate'] This will give the 4 Columns by 4 Rows Character Array: # Logical Indexing of a Character Array

We can use Logical Indexing on the Character Array. In essence MATLAB treats the Character Array Like the following: $\displaystyle \text{MyCharCol=}\left[ {\begin{array}{*{20}{c}} {\text{ }\!\!'\!\!\text{ b }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ a }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ t }\!\!'\!\!\text{ }} & {'\text{ }'} \\ {\text{ }\!\!'\!\!\text{ c }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ a }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ t }\!\!'\!\!\text{ }} & {'\text{ }'} \\ {\text{ }\!\!'\!\!\text{ m }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ a }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ t }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ e }\!\!'\!\!\text{ }} \\ {\text{ }\!\!'\!\!\text{ f }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ a }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ t }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ e }\!\!'\!\!\text{ }} \end{array}} \right]$

If we are interested in a letter in a certain location: $\displaystyle \text{MyCharCol=}\left[ {\begin{array}{*{20}{c}} {\text{ }\!\!'\!\!\text{ b }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ a }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ t }\!\!'\!\!\text{ }} & {'\text{ }'} \\ {\text{ }\!\!'\!\!\text{ c }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ a }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ t }\!\!'\!\!\text{ }} & {'\text{ }'} \\ {\text{ }\!\!'\!\!\text{ m }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ a }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ t }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ e }\!\!'\!\!\text{ }} \\ {\text{ }\!\!'\!\!\text{ f }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ a }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\mathbf{ t }\!\!'\!\!\text{ }} & {\text{ }\!\!'\!\!\text{ e }\!\!'\!\!\text{ }} \end{array}} \right]$

Selection=MyText(4,3)

# Creating a Cell Array

However there is another type of Array Called a Cell Array which allows text to be input of different length. To construct a Cell Array we use the { } opposed to [ ]:

MyCellCol1={'bat';'cat';'mate';'fate'} $\displaystyle \text{My}\text{CellCol1=}\left\{ {\begin{array}{*{20}{c}} {'\text{bat}'} \\ {'\text{cat}'} \\ {'\text{mate}'} \\ {'\text{fate}'} \end{array}} \right\}$ This creates the 4 Columns by 1 Row Cell Array. Note how it looks more like a SpreadSheet and how each Cell in the Cell Array can contain a differing quantity of text characters: # Logical Indexing of a Cell Array

There is a subtle difference in the output when ( ) and { } brackets are used for logical indexing. Say for instance we are interested in the element on the second row and first column: $\displaystyle \text{My}\text{CellCol1=}\left\{ {\begin{array}{*{20}{c}} {'\text{bat}'} \\ {'\mathbf{cat}'} \\ {'\text{mate}'} \\ {'\text{fate}'} \end{array}} \right\}$

Using Circular Brackets:

SelectionWithCircular=MyCellCol1(2,1) $\displaystyle \text{SelectionWithCircular=}\left\{ {'\text{cat}'} \right\}$ We get an output that is a 1 Column by 1 Row Cell Array. On the other hand if we use Curly Brackets instead:

SelectionwithCurly=MyCellCol1{2,1} $\displaystyle \text{SelectionWithCurly=}'\text{cat}'$ We get an Output that is a 1 Column by 3 Rows Character. Indexing using () gives a result referring to the set of cells while indexing using { } refers to the text, numbers, or other data within individual cells.

Now supposing we want to select the first two elements from the 4 Rows by 1 Column Cell Array MyCellCol1: If we use Curly Brackets we should yield a result referring to the set of cells:

SelectionwithCircular2=MyCellCol1([1;2],1) $\displaystyle \text{SelectionWithCircular2=}\left\{ {\begin{array}{*{20}{c}} {'\text{bat}'} \\ {'\text{cat}'} \end{array}} \right\}$ We get a 2 Row by 1 Column Cell Array as expected: On the other hand if we use Curly Brackets we should refers to the text, numbers, or other data within individual cells.

SelectionwithCurly2=MyCellCol1{[1;2],1}

We are however given only one of these two set of Characters: $\displaystyle \text{SelectionWithCurly2=}'\text{bat}'$ Here we get a 1 Row by 3 Columns Character Array and get the text directly from the Array as expected. However we selected 2 Rows from MyCellCol1: In this type of indexing we have asked MATLAB to read two different cells which will give two different sets of Character Outputs however we have only specified one Output on the Left Hand Side of the Assignment Operator meaning the second Output is left unassigned. We can select multiple Outputs using [ ] brackets and separate the outputs using ,.

[SelectionwithCurly2A,SelectionwithCurly2B]=MyCellCol1{[1;2],1}

This now gives the two separate outputs as expected: $\displaystyle \text{SelectionWithCurly2A=}'\text{bat}'$ $\displaystyle \text{SelectionWithCurly2B=}'\text{cat}'$

# Concatenation of Cell Arrays

We can create another 4 by 1 Cell Column using:

MyCellCol2={'ball';'mouse';'friend';'destiny'} $\displaystyle \text{My}\text{CellCol2=}\left\{ {\begin{array}{*{20}{c}} {'\text{ball}'} \\ {'\text{mouse}'} \\ {'\text{friend}'} \\ {'\text{destiny}'} \end{array}} \right\}$ Now if we want to create a Cell Array of both columns we use:

MyCell=[MyCellCol1,MyCellCol2] $\displaystyle \text{My}\text{Cell1=}\left\{ {\begin{array}{*{20}{c}} {\text{ }\!\!'\!\!\text{ bat }\!\!'\!\!\text{ }} & {'\text{ball}'} \\ {'\text{cat}'} & {'\text{mouse}'} \\ {'\text{mate}'} & {'\text{friend}'} \\ {'\text{fate}'} & {'\text{destiny}'} \end{array}} \right\}$ This generates a 4 Column by 2 Row Cell Array as expected: # Creating a Nested Cell Arrays

You may be wondering why we are using [ ] to concatenate two Cell Columns together. If we use the { } we will instead create a nested Cell Array: $\displaystyle \text{My}\text{Cell2=}\left\{ {\left\{ {\begin{array}{*{20}{c}} {'\text{bat}'} \\ {'\text{cat}'} \\ {'\text{mate}'} \\ {'\text{fate}'} \end{array}} \right\},\left\{ {\begin{array}{*{20}{c}} {'\text{ball}'} \\ {'\text{mouse}'} \\ {'\text{friend}'} \\ {'\text{destiny}'} \end{array}} \right\}} \right\}$ This creates a 1 Row by 2 Columns Nested Array. Note the Cells of the Parent Cell Array themselves each contain a 4 Rows by 1 Column Array: This can be accessed by double clicking in the Parent Cell: # Logical Indexing of a Nested Cell Array

Recalling from earlier that indexing using ( ) yields a result referring to the set of cells while indexing using { } refers to the text, numbers, or other data within individual cells. Using circular brackets with the Nested Cell Array Above:

CircularSelectionFromMyCell2=MyCell2(1,1) Yields a Description that is presented in a Nested 1 Row by 1 Column Cell Parent and 4 Rows by 1 Column Child.
CircularSelectionFromMyCell2={4×1 cell} Clicking into the Child: Using the curly brackets on the other hand:

CurlySelectionFromMyCell2=MyCell2{1,1} Displays the contents i.e. gives a 4 Columns by 1 Row Cell Array as an Output: $\displaystyle \text{CurlySelectionFromMyCell2}=\left\{ {\begin{array}{*{20}{c}} {'\text{bat}'} \\ {'\text{cat}'} \\ {'\text{mate}'} \\ {'\text{fate}'} \end{array}} \right\}$ 