7.3: Creating 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.

    7.3: Creating Strings

    A string is a sequence of characters placed between double quotation marks. In MATLAB, a single piece of text stored as a string is often called a string scalar. For example to create the phrase Hello Worldwe enclose the phrase in double quotation marks.

     myString = "Hello World"
    

    The variable myString stores the text Hello World as a string. The space between Hello and World counts as a character too.

    String Arrays

    Just like we can create arrays of numbers, we can create arrays of strings. Each element of the array is a string. The following example creates a 2-by-2 string array.

    strArray = ["Mercury", "Jupiter"; "Earth", "Saturn"]

    The first row contains "Mercury" and "Jupiter". The second row contains "Earth" and "Saturn".

    We can index a string array the same way we index other MATLAB arrays.

    secondRow = strArray(2, :)
    

    The result is a 1-by-2 string array containing the strings from the second row.

    Exercise \(\PageIndex{1}\)
    % Create a string array that stores the names of three classes you are taking. Then extract only the second class name.
    
    % myClasses = 
    % secondClass =

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