7.8: Checking Text Types and Properties

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    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.8: Checking Text Types and Properties

    Sometimes a program needs to check what kind of text it has, or what kinds of characters appear inside the text. The functions isstring, ischar, and isStringScalar are used for this purpose.

    Function

    Returns true when...

    isstring(x)

    x is a string array.

    isStringScalar(x)

    x is a single string scalar.

    ischar(x)

    x is a character vector or character array.

    Exercise \(\PageIndex{1}\)
    Run the following commands and pay close attention to what MATLAB returns.
    
    sampleStr = "Hello World";
    
    sampleArr = 'Hello World';
    
    
    
    result_1 = isstring(sampleStr)
    
    result_2 =isstring(sampleArr)
    
    result_3 =ischar(sampleStr)
    
    result_4 = ischar(sampleArr)
    
    

    Checking Individual Character Properties

    The function isstrprop checks each character in a character vector and reports whether that character belongs to a category such as alphabetic, digit, lowercase, uppercase, or whitespace.

    Exercise \(\PageIndex{2}\)
    Run the following commands and pay attention to the results.
    
    
    arr = 'eKtM 123';
    
    result_1 = isstrprop(arr, 'alpha')
    
    result_2 = isstrprop(arr, 'alphanum')
    
    result_3 = isstrprop(arr, 'lower')
    
    result_4 = isstrprop(arr, 'digit')
    
    result_5 = isstrprop(arr, 'wspace')

    The result is a logical array. Each position in the result corresponds to one character in the original text. A 1 means true, and a 0 means false.

    The following table lists the isstrprop categories and their meaning.

    Category

    Meaning

    'alpha'

    Alphabetic letters.

    'alphanum'

    Letters or digits.

    'digit'

    Numeric digits such as 0, 1, 2, ..., 9.

    'lower'

    Lowercase letters.

    'upper'

    Uppercase letters.

    'wspace'

    Whitespace, such as spaces, tabs, and newline characters.

    Here are a few other property functions and what they do:

    Function

    What it does

    isletter Checks for alphabetic letters.
    isspace Check for whitespace characters
    iskeyword Checks whether the input is a reserved MATLAB keyword, such as for, if, or end.


    Exercise \(\PageIndex{3}\)
    Run the following commands and pay attention to the results.
    
    
    arr = 'ek 123 ';
    
    result_1 = isletter(arr)
    
    result_2 = isspace(arr)
    
    result_3 = iskeyword(arr)
    
    result_4 = iskeyword('for')

    This page titled 7.8: Checking Text Types and Properties was last modified on Tue, 30 Jun 2026 02:11:54 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.