Section 3.1 Strings as Arrays of Characters
MATLAB provides several ways to deal with text. One way to store text is as an array of characters, just like it is done in C. There are two subtle differences: In MATLAB, an array of characters is created using single quotes (recall that double quotes were used in C):
>> name = 'this is a string'
Since arrays know their own size and we never really worry about such things, we don’t have to worry about whether they are null-terminated or not, we just use them.
One can treat strings just like any other array type when addressing elements:
>> secret = 'this is a string'
secret =
this is a string
>> secret(6:9) = '****'
secret =
this **** string
Strings can be used in output commands to display text messages, format commands for plots, input arguments for functions and more.
Be careful of the following difference:
>> x = 345
x =
345
Here,
x
is simply a number (check the workspace window).>> x = '345'
x =
345
Here on the other hand,
x
is an array of characters (a character array).