A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.



Saturday, March 5, 2016

Managing Strings in MATLAB


A string is an array of characters. A character string can be created in MATLAB using single quotes ('). The assignment operator is used to assign the string to a variable. For example,
>> a = 'Electrical'
a =
MATLAB will consider this as a 1-by-10 character array. A string can have letters, digits, symbols, spaces. To type single quote in string, use two consecutive single quotes.
Examples:  'aB  ef', '3%fr2', 'abcd:21!', 'MATLAB', 'Allen''s book'
A one line string will be considered as a row vector. If the string contain numbers, that also will be considered as characters. Number of elements in vector is number of characters in the string.
>> b = 'Electrical Engineering';
>> size(b)
ans =
          1 22
Color in code in editor
When you start type a string with a single quote it is colored maroon. When you complete the string with the closing quotation mark ('), it becomes purple.

Strings with Multiple Lines

A MATLAB variable can store strings with multiple lines as an array. This can be done similar the way we enter the matrices. The strings are entered in the square matrix and each string is separated by semi colon (;), but each line must have the same number of characters. If the lengths are not same, the remaining space must be padded with extra characters (usually spaces) to match with the longest string before closing the single quote.
>> c = [ 'Electrical'; 'Engineering' ]
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
>> c=[ 'Electrical '; 'Engineering' ]
c =
>> size(c )
ans =
          2 11
We can use ‘char’ function, which pads each line on the right with enough spaces so that all lines have the same number of characters
char('string 1', 'string 2', 'string 3')


Concatenation with square brackets joins text variables together into larger strings. The statement below joins two strings horizontally
a= 'Electrical'
d = [a, ' Engineering']     % note the space before E, used for space between the words. If space is not required don’t put the space.
d =
Electrical Engineering
The statement below joins the strings vertically
>> e = [a,' '; 'Engineering']
e =
Note the blank (‘ ‘) inserted after a, before the semi-colon. This space is important to make both strings have the same length.
The ‘char’ function can be used for vertical concatenation also.
>> f=char('Electrical', 'Engineering', 'Tutorial')
f =
First and third rows above have enough space characters added on ends to make each row 11 characters long

Indexing in String

Strings are indexed the same way as vectors and matrices
      Can read by index
      Can write by index
      Can delete by index
Consider the string a='Electrical',
>> a(1)
ans = E
>> a(7:end)
ans = ical
>> a(7) =’o’
a =
>> a(8:end) = []
>> a(end+1:end+4) = 'nics'
a =

String Comparison

There are several ways to compare strings and substrings:
  • You can compare two strings, or parts of two strings, for equality.
  •  You can compare individual characters in two strings for equality.
  • You can categorize every element within a string, determining whether each element is a character or white space.

strcmp is the function for comparing strings for equality. strncmp determines if the first n characters of two strings are identical. strcmpi function compares the strings without sensitivity to letter case.
Syntax is
x = strcmp('str1', 'str2')
x = strcmp('str', C)

strcmp('str1', 'str2') compares the strings str1 and str2 and returns logical 1 (true) if they are identical, and returns logical 0 (false) otherwise. str1 and str2 can be character arrays of any dimension, but strcmp does not return true unless the sizes of both arrays are equal, and the contents of the two arrays are the same.

strcmp('str', C) compares string str to the each element of cell array C, where str is a character vector (or a 1-by-1 cell array) and C is a cell array of strings. The function returns a logical array that is the same size as C and contains logical 1 (true) for those elements of C that are a match, and logical 0 (false) for those elements that are not. The order of the first two input arguments is not important.
strcmp('Electrical', 'Electrical')
ans =
strcmp('Electrical', 'Electronics')
ans =
g = {'Electrical';'Engineering'};
h = {'Electronic';'Engineering'};
strcmp(g, h)
ans =

Other Function on Strings

char(x) converts the array x that contains nonnegative integers representing character codes into a character array. Char function is used to vertically concatenate the strings.

lower('str') is used to convert any uppercase characters in str to the corresponding lowercase characters leaving all other characters unchanged.
>> lower('Electrical Engineering')
ans =
electrical engineering

upper('str') is used to convert any lowercase characters in str to the corresponding uppercase characters leaving all other characters unchanged.
>> upper('Electrical Engineering')
ans =

blanks         Create a string of blank characters
mat2str - convert matrix to string
str = mat2str(A) converts matrix A into a string
str = mat2str(A,n) converts matrix A using n digits of precision.

str2num – convert ASCII character to corresponding numeric value also converts string matrices to numeric matrices.
x = str2num('str') converts the string str (ASCII character representation of a numeric value) to numeric representation.

Functions for identifying parts of strings

ischar          Determine whether item is character array
isletter         Array elements that are alphabetic letters
isspace        Array elements that are space characters

submit to reddit