Lesson 2.3

Working with Strings


In this lesson we will discuss a few common and useful string manipulation MATLAB functions and techniques. However, there are many more MATLAB string manipulation functions available than we go over in this lesson.

Joining/Concatenating Strings

In some cases, it is useful to combine two strings together. For example, if you are given data with someone’s first and last names, but you want one string with their full name. This requires you to join or concatenate together. There are multiple ways you can join two or more strings into one string, here we present two options: the square bracket method ([]) and the horzcat() function.

Example 1: Defining a Single Character
someText1 = 'Ey yo! '; %Assigning text/string to a variable someText2 = 'What''s up?!'; %Assigning text/string to a variable %Method 1 joinedText1 = [someText1, someText2] %Using brackets to join two strings %Method 2 joinedText2 = horzcat(someText1,someText2) %Using horzcat MATLAB command to join two strings Command Window Output
joinedText1 = Ey yo! What’s up?! joinedText2 = Ey yo! What’s up?! [Try this code yourself with Octave Online! Click Here]

Searching and Counting Substrings

This section gives you some examples of how MATLAB can analyze strings. We will focus on only two functions, but you can refer to MATLAB documentation for characters and strings to see a full list.

If you are presented with a lengthy piece of text and would like to see if a word or phrase is contained within that lengthy piece of text, you can use the contains() function. Another example of analyzing a string is using the count() function to count how many times a word or phrase occurs within the text, you can use.

Important Note: The contains() function will return a boolean (true or false) value. It will return a 1 for true if it did find a match, and a 0 for false if it did not find a match.

Example 2: Searching and Counting Strings
someText = 'That gray dog is chasing that yellow dog.'; %Assigning text to a variable isDogGray = contains(someText,'gray') %Checking if the text contains the word 'gray' dogsInvolved = count(someText,'dog') %Counting the number of times the word 'dog' appears Command Window Output
isDogGray = 1 dogsInvolved = 2 [Try this code yourself with Octave Online! Click Here]

Lower and Upper Case

This section and the follow one are a few examples of manipulating strings with MATLAB. Refer to MATLAB documentation for characters and strings to see a full list.

Sometimes it is necessary to convert a string to all lower case or all UPPER case letters. This may be particularly useful when comparing two user input strings to each other such as 'Yes' and 'yes'. MATLAB offers the lower() and upper() functions to accomplish this.

Example 3: Changing all Characters to Upper/Lower Case
someText = 'How Is It Going, SmOOth cAt?'; %Assigning a piece of text to a variable someTextLow = lower(someText) %Converting text to all lower case someTextUp = upper(someText) %Converting text to all upper case Command Window Output
someTextLow = how is it going, smooth cat? someTextUp = HOW IS IT GOING, SMOOTH CAT? [Try this code yourself with Octave Online! Click Here]

Splitting/Parsing Strings

Often, it is useful to be able to split (or parse) strings when processing data using the MATLAB function strsplit(). This is because data can be separated (or “delimited”) by characters like a comma, space, or tab.

Example 4: Splitting Strings
dataInput1 = 'sup,5,3,yes,no,54.0'; %Assigning a piece of text to a variable dataSplit1 = strsplit(dataInput1,',') %Separating the string with commas (,) dataInput2 = 'MATLAB does some cool stuff!'; %Assigning a piece of text to a variable dataSplit2 = strsplit(dataInput2,‘ ’) %Separating the string with spaces ( ) Command Window Output
dataSplit1 = 'sup' '5' '3' 'no' 'yes' '54.0' dataSplit2 = 'MATLAB' 'does' 'some' 'cool' 'stuff!' [Try this code yourself with Octave Online! Click Here]

Using strsplit() will produce an array of cell data types. A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. It is ok if that sounds intimidating or you simply do not understand it since MATLAB data types will be covered in a subsequent lesson. However, what is important to know now is that you have to extract your data from each cell in order to use it. In order to reference a specific cell data in each cell array, you will have to use curly braces ({}).

Important Note: The data that is read from each cell array is a string even if it is shown as a number (i.e., '1'). (Note: Instead of using strsplit(), you may also use the function split(), which outputs straight to a string array instead of a cell array. However, note that split() is only available for MATLAB version 2016b and newer.)

Example 5: Using Data in Cell From Split Strings
dataInput = '1;2;3;4;5;6'; %Assigning a piece of text to a variable dataSplit = strsplit(dataInput,';') %Separating the string with semi-colons (;) dataSplit{1} %Outputting the first item from the split Command Window Output
dataSplit = '1' '2' '3' '4' '5' '6' ans = 1 [Try this code yourself with Octave Online! Click Here]

Next Lesson

In the next lesson, we will cover what the inputs and outputs of a program are and how to use them in MATLAB.