# Module 2: BASIC PROGRAMMING FUNDAMENTALS

## Learning Objectives

1. concatenate (join) strings,

2. analyze and manipulate 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 can cover in this lesson.

## How do I join two strings together?

The programmer can take multiple strings (that are assigned to variables) and combine them – this is called concatenation. An example of concatenation would be to combine several words to form a single sentence. Example 1 shows the use of string concatenation.

### Example 1

Assign the following two strings to two variables named, str1 and str2, in a new m-file.

string 1 = 'Strings can'

string 2 = ' tie a knot.'

Concatenate the two input strings to form a single sentence and show this sentence in the Command Window.

Notice the brackets used in the concatenation line (last line) of the m-file. This is required to join the two strings together in this method (there are other methods we do not cover here). The same process is also used if more than two strings are to be joined together.

It is important that you do not try to concatenate strings and standard numerals or numeric variables as it will not properly work. You can only join strings together to form new strings.

## How do I search and count strings?

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 to count how many times a word or phrase occurs within the text by using the count() function.

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

You are given the sentence, That gray dog is chasing that yellow dog.

Check whether the sentence (string) contains the word (string) gray and count how many times the word (string) dog occurs in the sentence.

## How do I make a whole string lower or upper case?

This section and the following one are a few examples of manipulating strings with MATLAB. You can see a full list of the different functions for string manipulation MATLAB offers by reviewing the MATLAB documentation for characters and strings.

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

You are given the sentence, How Is It Going, SmOOth cAt?

Convert the sentence (string) to all upper case letters (characters) and store it in a variable. Repeat the process for lower case.

## Can I split a string into its component pieces?

Often, it is useful to be able to split (or parse) strings when processing data and this can be done by using the MATLAB function strsplit(). Data has to be separated (delimited) by characters like a comma, space, or tab for strsplit() to work.

### Example 4

Split a single string, ‘sup,5,3,yes,no,54.0’, into multiple strings (pieces) based on the comma delimiter. Next, split the string, ‘MATLAB does some cool stuff!’, into multiple strings (words) based on the space delimiter.

Solution

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

### Example 5

Split a single string into multiple strings based on a delimiter.

Solution

The delimiter in the input string below is ; (semi-colon).

## Lesson Summary of New Syntax and Programming Tools

Join (concatenate) strings together [] ['string1', 'string2']
Count the number of occurrences of a pattern inside another string count() count('the dog is in the yard', 'the')
Check whether or not a string contains the pattern string contains() contains('the dog is running', 'dog')
Convert all letters in a string to upper case upper() upper('tHis TeXt')
Convert all letters in a string to lower case lower() lower('tHis TeXt')
Split a string into pieces output as a cell array strsplit() strsplit('these;are;words', ';')
Split a string into pieces output as a string array split() split('these are words')

## Multiple Choice Quiz

(1). The output of cat = ['cat' 'dog'] is

(a) catdog

(b) cat dog

(c) cat_dog

(d) CatDog

(2). The output of the last line is

(a)  cat2

(b)  cat 2

(c)  Undefined function or variable 'as'.

(d)  cat as

(3). To count the number of instances that one string occurs in another string (pattern), one should use the function

(a)  contains()

(b)  count()

(c)  check()

(d)  howmany()

(4). To separate the phrases or pieces of a string, one should use the function

(a)  phrases()

(b)  pieces()

(c)  comp()

(d)  strsplit()

(5). To search a string to see whether in contains another string (pattern), one should use the function

(a)  search()

(b)  contains()

(c)  ismember()

(d)  find()

## Problem Set

(1). Concatenate the following strings:

cm1 = 'My name is'
cm2 = 'Slim Shady.'

How would you change the string(s) so that there is proper spacing between each word?

(2). Count how many times the character ‘e’ occurs in the string 'Programming is important for mechanical engineers.'

(3). Check whether each of the following strings contains the corresponding patterns.

String: 'My name is Slim Shady.'              Pattern: 'name'

String: 'My name is Slim Shady.'              Pattern: 'Eminem'

String: 'The United States of America'       Pattern: 'Unit'

(4). Store the sentence “Watch out!” in a variable, and then convert it to all upper case letters.

(5). Split the string 'The,United,States,of,America' into its pieces using the strsplit() function and the comma delimiter.