askvity

What Is a Text Scalar in MATLAB?

Published in MATLAB Text Scalar 3 mins read

In MATLAB, a text scalar represents a single piece of text. While the core concept is simple, its specific definition can vary slightly depending on the data type used to store the text.

Understanding Text Scalars

At its heart, a text scalar is about containing one distinct textual value. This is analogous to a numerical scalar which holds a single number (like 5 or 3.14).

Definition Varies by Data Type

The primary way text is handled in modern MATLAB is through string arrays. For string arrays, a text scalar is defined as a 1-by-1 scalar string. This means it's an array with one row and one column containing a single string element.

  • Example: "hello" is a text scalar because it's a 1x1 string array containing the text "hello".

Special Cases

The definition includes special instances that still count as single pieces of text:

  • The empty string (""): This represents a piece of text that contains no characters. It is still considered a single, albeit empty, textual value.
  • Missing strings: These represent absent or undefined textual data within a string array. They are placeholders for missing information but are treated as single entities for scalar definition purposes.

Practical Examples

Let's look at some examples in MATLAB:

  • Creating a String Scalar:

    myScalarText = "This is a scalar string";
    % Who is myScalarText? string 1x1

    Here, myScalarText holds one piece of text and is a 1x1 string array.

  • The Empty String Scalar:

    emptyScalarText = "";
    % Who is emptyScalarText? string 1x1

    Even though it contains no characters, emptyScalarText is a 1x1 string array representing a single empty piece of text.

  • A Missing String Scalar:

    missingScalarText = strings(1,1); % Creates a 1x1 string array with a missing value
    % Who is missingScalarText? string 1x1
    % missingScalarText looks like <missing>

    This creates a 1x1 string array containing the special missing string value, which is also considered a scalar.

What is NOT a Text Scalar?

Any string array that is not 1-by-1 is not a text scalar.

  • Example:
    myStringArray = ["apple", "banana", "cherry"];
    % Who is myStringArray? string 1x3

    myStringArray is a 1x3 string array containing three pieces of text, not one. Therefore, it is not a text scalar. Similarly, a 2x2 string array or any other dimension besides 1x1 would not be a scalar.

In summary, a text scalar in MATLAB, particularly within the context of string arrays, is simply a 1-by-1 string array containing a single piece of text, including the special cases of the empty string and missing strings.

Related Articles