1.9.2: Texts - Characters and Strings

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    1.9.2: Texts - Characters and Strings

    MATLAB can store text in more than one way. The two types beginners see first are character arrays and strings.

    Text type

    How to create it

    Example

    Character array

    Use single quotes.

    txt1 = 'I love MATLAB!'

    String

    Use double quotes.

    txt2 = "I love MATLAB!"

    Example \(\PageIndex{1}\)

    Using whos

    txt1 = 'I love MATLAB!';
    txt2 = "I love MATLAB!";
    whos

    Solution

    Name     Size         Bytes          Class     Attributes
    
    txt1     1x14         28             char
    txt2     1x1         182             string
     

    Character Variables and ASCII

    A character variable stores text that is placed inside single quotation marks. A single character might look like 'A', 'x', or '&'. A sequence of characters placed inside single quotation marks is called a character array. For example, 'hello' is not one object containing a word in the same way a string scalar is; it is an array of five characters.

    This distinction can feel subtle at first, but it is important. MATLAB was originally designed around arrays, so character arrays behave like arrays. Each character has a position, or index, and MATLAB can access individual characters using indexing just like it accesses elements of a numerical array.

    Example \(\PageIndex{2}\)
    word = 'MATLAB';
    
    
    result_1 = word(1)
    result_2 = word(3)
    result_3 = word(end)
    Solution
    result_1 = 'M'
    
    result_2 = 'T'
    
    result_3 = 'B'

    So how does a computer store a character such as M or T? Internally, a computer stores information using numbers. One traditional system used to represent characters as numbers is called ASCII, which stands for American Standard Code for Information Interchange. In ASCII, each common character is assigned a numeric code. For example, uppercase letters, lowercase letters, digits, punctuation marks, and some control characters all have corresponding numeric values.

    MATLAB can reveal these numeric codes when you convert characters to numeric values.

    Example \(\PageIndex{3}\)
    double('A')
    double('a')
    double('0')
    double('MATLAB')
    Solution
    ans =
    
        65
    
    
    ans =
    
        97
    
    
    ans =
    
        48
    
    
    ans =
    
        77    65    84    76    65    66

    The uppercase letter A and lowercase letter a have different numeric codes. This is one reason MATLAB treats 'A' and 'a' as different characters. The character '0' is also not the same as the number 0. The character '0' is a text symbol that represents the digit zero, while the number 0 is a numerical value that can be used directly in arithmetic.

    This explains why the following example may surprise you:

    Example \(\PageIndex{4}\)
    'a' + 'b'
    Solution
    ans = 195

    MATLAB is not joining the two characters together. Instead, MATLAB converts the characters to their underlying numeric codes and then adds those codes. This is useful to know because it reminds us that text and numbers are stored differently, even when they look similar on the screen.

    You can also go in the opposite direction by converting a number to a character.

    Example \(\PageIndex{5}\)
    char(65)
    char(97)
    char(48)
    Solution
    ans = A
    ans = a
    ans = 0

    These commands return the characters associated with those numeric codes. For beginning MATLAB programming, you do not need to memorize ASCII codes. The important idea is that characters are stored using numeric representations, and MATLAB can sometimes treat characters as numbers because of that internal representation.

    Character array versus string: single quotes create character arrays, while double quotes create string scalars. For modern beginner-friendly MATLAB code, double quotes are often easier when you simply want to store or display text. Character arrays are still common in older MATLAB code and in some functions, so you should be comfortable recognizing both.

    Example \(\PageIndex{6}\)

    Compare these two variables in MATLAB:

    txt1 = 'I love MATLAB!'
    txt2 = "I love MATLAB!"
    whos
    
    Solution
    txt1 =
    
    'I love MATLAB!'
    
    
    txt2 =
    
    "I love MATLAB!"
    
    Name     Size     Bytes     Class Attributes
    
    txt1     1x14     28        char
    txt2     1x1     182        string

    he texts look almost the same when displayed, but whos shows that txt1 and txt2 have different data types. This is why it is important to pay attention to quotation marks in MATLAB.

    Long Text and Line Continuation

    If a long text value cannot comfortably fit on one line, you can use brackets and the continuation operator, which is three dots: ...

    newString = ['Never give up. Today is hard, tomorrow will be worse, ' ...
                 'but the day after tomorrow will be sunshine.']
    
    

    This page titled 1.9.2: Texts - Characters and Strings was last modified on Tue, 30 Jun 2026 02:12:03 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.