MATLAB split Function: Syntax and Examples

Formulas & functions · Updated November 22, 2026

MATLAB’s split function separates text into pieces at a delimiter and returns the pieces as a string array. Use parts = split(text, delimiter) for a specified separator, or parts = split(text) to split at whitespace; for example, split("red,green,blue", ",") produces the three strings "red", "green", and "blue".

What MATLAB’s split function does

split is a text-processing function for dividing a string into substrings. The separator itself is removed from the returned pieces. This is useful when a value contains fields separated by a consistent marker, such as commas in a short list, hyphens in an identifier, or spaces between words. The result is a string array, so each piece can be indexed, compared, or passed to other string-aware MATLAB functions.

The function is not a general-purpose file parser. A comma-separated line, for example, might contain quoted commas, escaped quotation marks, or missing fields. In those cases, simply splitting on commas may not interpret the data according to the file format. For structured files, use a suitable import function such as readtable or readmatrix rather than treating every line as uncomplicated text.

It also helps to distinguish splitting from searching. split returns the text segments; it does not return the positions of the delimiters. If you need delimiter locations, consider functions such as strfind or a regular expression function instead.

Basic syntax and a first example

The common forms are parts = split(text), parts = split(text, delimiter), and parts = split(text, delimiter, dim). The first form uses whitespace as the separator. The second uses the delimiter you provide. The optional dimension controls the dimension along which splitting is performed when working with arrays.

For a string scalar, a simple example is:

text = "north|east|south";
parts = split(text, "|");

Here, parts is a string array containing "north", "east", and "south". You can select an element with ordinary array indexing, such as parts(2), which is "east". Use parentheses to index a string array; curly braces are used for cell arrays, not for this result.

For a small list with spaces, omit the delimiter:

words = split("MATLAB handles string arrays");

This splits at whitespace and returns the words as string elements. If you need to split on a particular character, state it explicitly rather than relying on whitespace behavior. A tab, comma, slash, or colon is not interchangeable with a space.

String arrays, character vectors, and output shape

MATLAB has both string data and character data. Double quotes create strings, while single quotes create character vectors: "sample text" is a string scalar, and 'sample text' is a character vector. The clearest way to use split is to work with strings. If your input is legacy character data, convert it explicitly with string when appropriate:

oldText = 'oak,maple,birch';
parts = split(string(oldText), ",");

This makes the input type and the string-array result clear. When handling character data in older code, strsplit may be a better fit because it returns a cell array of character vectors. The two functions are related, but their input and output types are not identical; code that expects a cell array should not silently be changed to expect a string array.

For a string scalar, splitting usually gives a column of pieces. If you split an array containing multiple strings, the dimensions of the input and the number of pieces per element affect the result. The optional dim argument lets you specify the dimension along which to split. Because shape matters in later calculations, check it with size(parts) and inspect the result in MATLAB rather than assuming every input arrangement produces a row or column in the same way.

Choosing a delimiter

A delimiter is the text that marks a boundary between pieces. It can be a single character, such as a comma, or a longer text sequence, such as "--". A multi-character delimiter is treated as the separator text you specify; it is not a regular expression pattern. For example, to split a code at a double colon, use:

fields = split("area::zone::unit", "::");

If you have several literal separators that should all count as boundaries, you can supply a string array of delimiters. For instance, a text value may use either a comma or a semicolon. Make the intended separators explicit, and test with representative inputs so that punctuation inside a field is not accidentally treated as a boundary.

With no delimiter argument, split uses whitespace. That is handy for simple text, but it may not express a more specific rule. For example, splitting a timestamp on spaces does not separate its date components, and splitting a sentence on spaces does not remove punctuation attached to words. Choose the delimiter that matches the structure you actually need to extract.

Practical examples: fields, numbers, and arrays

To extract fields from a simple identifier, split on its known separator and then select the part you need:

id = "station-42-west";
fields = split(id, "-");
stationNumber = fields(2);

Here, stationNumber is the string "42". It is still text, not a numeric value. Convert it only if the next operation requires a number. For a single value in that position, str2double(stationNumber) returns the numeric value 42. For a string array of numeric pieces, str2double can convert the elements; check for NaN if the input might contain nonnumeric text.

A compact example for a list of measurements stored in text is:

text = "12.5,18.0,20.25";
valuesText = split(text, ",");
values = str2double(valuesText);

The resulting numeric array contains 12.5, 18.0, and 20.25. This approach is suitable when the input is known to contain plain numeric fields separated by commas. It is not a substitute for validating units, missing values, or more complicated data formats.

You can also apply split to each element of a string array. For example, splitting several labels can be useful when each label follows a shared pattern. If labels do not all contain the same number of fields, take care when using the result in later array operations: inspect its size and missing entries, and do not assume every row has a meaningful value at every position.

split compared with strsplit, splitlines, and regexp

Choose a text function based on the input type and the rule that defines a boundary:

  • split is a natural choice for string data and returns string-array pieces. Use it for straightforward literal delimiters or whitespace.
  • strsplit is useful when existing code uses character vectors and cell arrays. It also provides options for controlling delimiter handling, so it can be useful when empty fields or repeated delimiters need specific treatment.
  • splitlines is designed to divide text at line boundaries. It communicates the intent more directly than manually splitting on a particular newline character.
  • regexp and related regular-expression functions are appropriate when the separator is a pattern, such as a run of digits or several alternative boundary rules. A regular expression has special syntax, unlike a literal delimiter passed to split.

For example, use split("one,two,three", ",") for a simple comma-separated string. If the rule is “split at one or more commas or spaces,” that is a pattern-based requirement and a regular expression may be more suitable. If the text is a real CSV record where quoted fields may contain commas, use a CSV-aware import workflow instead of either simple split form.

Common problems and ways to avoid them

A frequent source of confusion is the difference between a string and a character vector. MATLAB displays them differently and gives them different types. If a function later in a script expects a cell array of character vectors, a string-array result from split may not be a drop-in replacement. Check types with class, and convert deliberately rather than relying on implicit behavior.

Another common issue is assuming that the output is numeric because the input looks numeric. Splitting "8,9,10" produces text pieces. Use str2double for conversion, then validate the result. For example, a misspelled field such as "nine" cannot be converted to a number and will not produce the numeric value 9.

Be particularly careful with empty fields. Repeated delimiters, or a delimiter at the start or end of text, can matter when the positions of blank fields carry meaning. Do not assume a simple split has the exact behavior required by a data format. Try a short test containing repeated, leading, and trailing separators. If preserving empty fields is essential, review strsplit and its CollapseDelimiters option, or use a parser designed for the format.

Finally, remember that split does literal delimiter splitting, not pattern matching. Passing a period as the delimiter means a literal period, not “any character.” This differs from regular-expression syntax, where a period has a special meaning unless escaped. If your boundary rule involves patterns, choose a regular-expression function and write the pattern accordingly.

Frequently Asked Questions

What does split do in MATLAB?

It separates text at whitespace or at a delimiter you specify, removes the separators from the returned pieces, and returns those pieces as a string array. It is intended for dividing text, not for automatically converting the pieces to numbers.

How do I split a string by a comma?

Use parts = split(text, ","), where text is the string you want to divide. For example, split("red,green,blue", ",") returns the three color names as string elements.

How do I split a string by spaces?

Use parts = split(text) to split at whitespace. If the boundary is specifically another character, such as a tab or comma, pass that delimiter explicitly so the code clearly expresses the intended rule.

Does split return a cell array?

No. split returns a string array. strsplit returns a cell array of character vectors, which can suit older MATLAB code or workflows built around cell arrays.

How can I turn split pieces into numbers?

First split the text, then pass the resulting numeric strings to str2double. Check for NaN or otherwise validate the conversion, since any piece that is not a valid numeric representation cannot become the intended number.

Choosing the right approach

For clean text with a known literal separator, split is a concise and readable choice. Use its default whitespace behavior for plain word-like text, provide an explicit delimiter for structured fields, and inspect the output type and dimensions when working with arrays. If the input is character-based, the surrounding code expects cells, the separator is a pattern, or the text follows a formal file format, choose strsplit, a regular-expression function, or a format-aware import function as appropriate. Testing a few realistic edge cases before applying the operation to a large dataset can prevent subtle parsing errors.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.