How to Split a String With Whitespace in MATLAB

Save

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

References

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

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

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