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

  • How to Split the Matlab Jet Color Table to RGB

    MATLAB is a software package produced by MathWorks that enables users to build upon a large library of functions for technical analysis...

  • How to Parse Sentences

    Parsing sentences involves identifying the function of each word. Formal English grammar used to be taught in school regularly in the belief...

  • How to Insert Into an Array in Matlab

    MATLAB is a technical computing program used in science and engineering for data processing and analysis. Arrays and matrices store data in...

  • How to Print a String in MATLAB

    Basic MATLAB programming skills are a necessity for students in different disciplines from economics to physics. Engineers and mathematicians can also make...

  • How to Display in Tabular Format in MATLAB

    For most people familiar with Excel or other table-based spreadsheet software, it's much easier to work with database values in tabular format...

  • How to Find a Substring in MATLAB

    Strings of text are stored in MATLAB as vectors (single-row matrices). Individual characters in strings can be retrieved just as elements in...

  • How to Plot Multiple Plots in MATLAB

    MATLAB is a language designed for technical computing that includes an interactive, graphical environment . The "Figures" window in the MATLAB desktop...

  • How to Plot a Function in MATLAB

    MATLAB is a digital tool, and as such can only work with finite subsets of the domains and ranges of real functions....

  • How to Turn Input Into a Vector in MATLAB

    MATLAB is capable of performing powerful computations but also offers many of the conveniences common to high-level programming languages, like receiving and...

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!