MATLAB Strings - MATLAB

What is MATLAB Strings?

Creating a character string is quite simple in MATLAB. In fact, we have used it many times. For example, you type the following in the command prompt –

MATLAB will execute the above statement and return the following result –

MATLAB considers all variables as arrays, and strings are considered as character arrays. Let us use the whos command to check the variable created above –

MATLAB will execute the above statement and return the following result –

Interestingly, you may use numeric conversion features like uint8 or uint16 to convert the characters in the string to their numeric codes. The char function converts the integer vector returned to characters −

Example

Create a script file and type the following code into it –

When you run the file, it displays the following result –

Rectangular character Array

The strings we have mentioned so far are one-dimensional character arrays; however, we need to store more than that. We want to store more dimensional textual information in our program. that is completed by developing rectangular character arrays.

Simplest way of creating a rectangular character array is through concatenating or more one-dimensional character arrays, both vertically or horizontally as required.

You can combine strings vertically in either of the following methods −

  • Using the MATLAB concatenation operator [] and separating each row with a semicolon (;). Please note that in this method every row must contain the same number of characters. For strings with specific lengths, you should pad with area characters as needed.
  • Using the char function. If the strings are of different lengths, char pads the shorter strings with trailing blanks so that each row has the same number of characters.

Example

Create a script file and type the following code into it –

When you run the file, it displays the following result –

you can combine strings horizontally in either of the following methods −

  • Using the MATLAB concatenation operator, [] and separating the input strings with a comma or a space. This technique preserves any trailing spaces in the input arrays.
  • Using the string concatenation function, strcat. This method removes trailing spaces in the inputs.

Example

Create a script file and type the following code into it –


When you run the file, it displays the following result –
##ESTART##
profile = Zara Ali , Sr. Surgeon , R N Tagore Cardiology Research Center
profile = Zara Ali,Sr. Surgeon,R N Tagore Cardiology Research Center

Combining Strings into a Cell Array

From our previous discussion, it is clear that combining strings with unique lengths could be a pain as all strings in the array has to be of the same length. we have used blank spaces at the end of strings to equalize their length.

However, a more efficient way to combine the strings is to convert the resulting array into a cell array.

MATLAB cell array can hold unique sizes and types of data in an array. cell arrays offer a extra flexible way to store strings of varying length.

The cellstr feature converts a character array into a cell array of strings.

Example

Create a script file and type the following code into it –

When you run the file, it displays the following result –

String Functions in MATLAB

MATLAB provides numerous string functions creating, combining, parsing, comparing and manipulating strings.

Following table provides brief description of the string functions in MATLAB –

Function

Purpose

Functions for storing text in character arrays, combine character arrays, etc.

blanks

Create string of blank characters

cellstr

Create cell array of strings from character array

char

Convert to character array (string)

iscellstr

Determine whether input is cell array of strings

ischar

Determine whether item is character array

sprintf

Format data into string

strcat

Concatenate strings horizontally

strjoin

Join strings in cell array into single string

Functions for identifying parts of strings, find and replace substrings

ischar

Determine whether item is character array

isletter

Array elements that are alphabetic letters

isspace

Array elements that are space characters

isstrprop

Determine whether string is of specified category

sscanf

Read formatted data from string

strfind

Find one string within another

strrep

Find and replace substring

strsplit

Split string at specified delimiter

strtok

Selected parts of string

validatestring

Check validity of text string

symvar

Determine symbolic variables in expression

regexp

Match regular expression (case sensitive)

regexpi

Match regular expression (case insensitive)

regexprep

Replace string using regular expression

regexptranslate

Translate string into regular expression

Functions for string comparison

strcmp

Compare strings (case sensitive)

strcmpi

Compare strings (case insensitive)

strncmp

Compare first n characters of strings (case sensitive)

strncmpi

Compare first n characters of strings (case insensitive)

Functions for changing string to upper- or lowercase, creating or removing white space

deblank

Strip trailing blanks from end of string

strtrim

Remove leading and trailing white space from string

lower

Convert string to lowercase

upper

Convert string to uppercase

strjust

Justify character array

Examples

The following examples illustrate some of the above-mentioned string functions −

FORMATTING STRINGS

Create a script file and type the following code into it –


When you run the file, it displays the following result −

JOINING STRINGS

Create a script file and type the following code into it –


When you run the file, it displays the following result –


FINDING AND REPLACING STRINGS

Create a script file and type the following code into it –


When you run the file, it displays the following result –

COMPARING STRINGS

Create a script file and type the following code into it –

When you run the file, it displays the following result –

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

MATLAB Topics