How to split a string with whitespace in matlab

Written by michael carroll
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to split a string with whitespace in matlab
Parse text in MATLAB with "textscan." (Hemera Technologies/ Images)

MATLAB contains a number of functions to facilitate working with text strings. A set of words separated by whitespace is a common type of string, and numerical data might also appear in a whitespace-separated string. The "textscan" function splits such a string into separate substrings if you use the "%s" format argument.

Skill level:

Other People Are Reading


  1. 1

    Create a string to test "textscan" on by typing the following command into the Command Window:

    teststring = 'The quick brown fox jumps over the lazy dog.'

    Be sure to use single quotation marks when defining strings in MATLAB.

  2. 2

    Assign the results of "textscan" to a variable to split "teststring" according to its whitespace:

    splitstring = textscan(teststring,'%s')

    A cell array is assigned to "splitstring," and its first element contains the split portions of "teststring" as a column vector.

  3. 3

    Use the following command to extract the values from the cell array returned by "textscan," making it easier to access the strings:

    splitstring = splitstring{1}

    The variable "splitstring" now contains a column vector containing the strings that were split by "textscan."

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.