How to Split a String With Whitespace in MATLAB


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.

  • 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.

  • 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.

  • 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."

Related Searches


  • Photo Credit Hemera Technologies/ Images
Promoted By Zergnet


You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!