Table of Contents
Overview
In MATLAB you will work with text in two main ways, as character arrays and as string arrays. Both store sequences of characters, but they behave differently, especially when you combine them, index them, or store multiple pieces of text together. Understanding this distinction early makes later text processing much easier.
Character arrays
A character array is a regular MATLAB array whose elements are of class char. Each element stores a single character, such as a letter, digit, or symbol. A simple line of text can be stored as a row vector of type char.
You create a character array by enclosing text in single quotes. For example, the command
c = 'Hello';creates a 1-by-5 character array. You can verify its class with
class(c)
size(c)
The size reports the number of characters. Each position in the array holds one character, so you can index into it in the same way you index numeric arrays. For instance, c(1) is 'H', and c(2:4) is 'ell'.
Character arrays can have multiple rows. To form a 2D character array, all rows must have the same number of columns. MATLAB pads shorter rows with spaces to achieve this. For example,
names = ['Ann '; 'Mark'];
creates a 2-by-4 character array. The first row becomes 'Ann ' with a trailing space so that both rows have length 4. When you view names, MATLAB prints
Ann
MarkThe space padding is part of the data and affects comparisons, length calculations, and other operations.
Creating character arrays
Character arrays can be created from literals, concatenation, and functions such as char. A literal is written directly with single quotes, for example 'MATLAB' or 'A'. Concatenation uses square brackets in the same way as for numeric arrays. The expression
x = ['a', 'b', 'c'];
creates the character array 'abc'. Concatenating along rows or columns follows the usual size rules. To combine two character vectors horizontally, their row counts must match. To stack them vertically, their column counts must match, which again can introduce padding.
The char function can convert numbers to corresponding Unicode code units. For instance, char(65) returns 'A'. It can also combine multiple character vectors into a padded character array:
C = char('cat','dog','bird');
Here C is a 3-row character array. The shorter words are padded with spaces so each row has the same number of characters as the longest word.
String arrays
String arrays are a newer text data type in MATLAB. Each element of a string array is a string scalar that represents a piece of text, not a single character. You create string scalars using double quotes. For example,
s = "Hello";
creates a string scalar. Its class is string. The variable s contains the entire word "Hello" as a single element, and not as a vector of individual character codes.
You create a string array by listing several strings in double quotes. For example,
animals = ["cat" "dog" "bird"];creates a string array with 1 row and 3 columns. Each entry is a separate string scalar. The size of this array is 1-by-3, independent of the lengths of the individual text values. There is no automatic padding with spaces. You can also create a column string array with semicolons,
cities = ["Paris"; "Rome"; "Berlin"];which results in a 3-by-1 string array.
String arrays are convenient when you handle collections of words, file names, labels, or any other textual values where each element may have a different length. Storage is handled internally and you do not see extra spaces added as in multirow character arrays.
Comparing character arrays and string arrays
Character arrays and string arrays differ in creation syntax, internal representation, and how they behave in arrays. Single quotes produce character arrays. Double quotes produce strings. The expression 'Hello' creates a 1-by-5 char array. The expression "Hello" creates a 1-by-1 string array whose single element stores the text.
When you have more than one piece of text, the differences become more obvious. A multirow character array such as
namesChar = char('Ann','Mark','Sue');is a 3-by-4 array. Each row has 4 characters, with padding where necessary. The string array version
namesStr = ["Ann","Mark","Sue"];
is a 1-by-3 array. Each element of namesStr stores a full piece of text. The three texts can each have a different length without affecting the array shape.
Indexing also behaves differently. With a character array, a single index returns characters. If c = 'Hello', then c(2) is the character 'e'. With a string array, a single index returns entire string elements. If s = ["Hello","World"], then s(2) is the string "World". To access the individual characters of a string element, you typically convert that element to a character array first, or work with functions that operate at the string level.
In arithmetic and size behavior, character arrays resemble ordinary numeric arrays. Their size is determined by the number of characters in each dimension. For string arrays, the size counts the number of text elements. The content length of a particular string does not affect the array dimensions.
Choosing between character arrays and string arrays
When you decide which text representation to use, consider how you will process the data. Character arrays fit naturally when you must treat text purely as sequences of characters, when you need direct character-by-character indexing, or when you interact with older MATLAB code that expects char inputs.
String arrays are usually more convenient for modern MATLAB work that involves sets of textual values, such as categories of data, labels, names, or file paths. You treat each textual piece as a single unit and let MATLAB manage varying lengths internally. Many recent functions in MATLAB that handle text accept or prefer string arrays.
You can have both types in the same workspace, but a single variable is either char or string. When you combine them in an expression, MATLAB applies rules to determine the resulting type. To keep your code clear, it is often helpful to choose one representation for each task and convert explicitly when needed.
Converting between types
Conversion functions let you switch between character arrays and string arrays. To convert a character array to a string array, use string. For example,
c = 'Hello';
s = string(c);
The variable s is now a string scalar "Hello". If c is a multirow character array, string(c) produces a string array where each row becomes one string element, and trailing spaces are removed automatically.
To convert a string array back to char, you typically convert individual elements. If you know a string scalar contains a single piece of text, you can write
s = "World";
c = char(s);
which returns the character array 'World'. If s is a string array with several elements, char(s) creates a padded multirow character array that combines them, similar to what happens when creating character arrays from multiple literals. Each string element becomes one row and MATLAB pads shorter rows with spaces so that all rows have the same length.
During conversion, be aware that whitespace padding is treated differently. Converting from multirow char to string removes trailing spaces, so the text content may look cleaner. Converting a string array with unequal text lengths into a character array introduces new trailing spaces to equalize row lengths.
Display and representation
In the Command Window, MATLAB displays character arrays without quotation marks. For example, entering a character array variable shows the text directly. String arrays display each element in double quotes. A scalar string appears like "text". A string array with several elements lists each one with quotes.
You can always check the underlying type with class. The function whos also reports variable classes and sizes, which lets you distinguish between char and string variables even if their printed text looks similar.
Because character arrays and string arrays both represent textual data, it is easy to confuse them, especially when reading code. Paying attention to single quotes versus double quotes helps you quickly identify which type you are using.
Important things to remember:
Use single quotes for character arrays and double quotes for string arrays.
Character arrays store each character as an element and multirow character arrays use space padding.
String arrays store each piece of text as one element and do not pad elements to equal length.
Convert between types with string and char, and be aware that padding may be added or removed during conversion.