In this article, you will learn to use text in a matrix.

## Defining a Matrix with Numbers and Strings

For example, let’s say you want to define the following matrix

$$m=\left(\begin{array}{ccc}column1 &column2&column3\\1&2&3\\4&5&6\end{array}\right)$$

Knowing that you can define strings (i.e. text) with single quotation marks, you could be tempted to do the following:

m = ['column1' 'column2' 'column3' ; 1 2 3 ; 4 5 6];

Let’s try to run this piece of code:

Why doesn’t this work?
MATLAB concatenates ‘column1’ with ‘column2’ with ‘column3’, which means that the first row has one column and the second and third rows have three columns:

## Using Cells

The solution is actually pretty simple once you know the trick: use cells to separate the content. Here’s an example of how to do this:

m = [{'column1'} {'column2'} {'column3'} ; {1} {2} {3} ; {4} {5} {6}]

To use a cell, you just have to write what you want between braces (e.g., {‘x’}). Then, if you want to access the content of one of the cells of the matrix, you have to use braces as well.

If you use parentheses, the output will be a cell instead of the content of a cell, which means you won’t be able to manipulate it (i.e. you won’t be able to do any operation on the number of the matrix m).

Key takeaways:

• The matrix definition:
m = ['column1' 'column2' 'column3' ; 1 2 3 ; 4 5 6];

doesn’t work because MATLAB concatenates ‘column1’ with ‘column2’ with ‘column3’.

• Instead, to mix strings and numbers in a matrix use cells:
m = [{'column1'} {'column2'} {'column3'} ; {1} {2} {3} ; {4} {5} {6}]