Some Common Matlab String Functions: Category Function Description Example

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

SOME COMMON MATLAB STRING FUNCTIONS

Category Function Description Example


General char (1) Convert numbers to the corresponding character >> char(97), ans = a
values.
(2) Create a two dimensional character array from a series >> string1='abcdef'; >> string2='abc';
of strings. >> char(string1,string2),
ans =
abcdef
abc
double Convert characters to the corresponding numeric codes. >> double ('a'), ans = 97
blanks Create a string of blanks. >> disp(['xxx' blanks(20) 'yyy'])
xxx yyy
deblank Remove trailing whitespace from a string. >> B{1,1}='Hello! ';
>> deblank(B)
ans =
'Hello!'
strtrim Remove leading and trailing whitespace from a string. >> B{1,1}=' Hello! ';
>> strtrim(B)
ans =
'Hello!'
String tests ischar Returns true (1) for a character array. >> ischar(12)
ans =
0
>> ischar(a)
ans =
1
isletter Returns true (1) for letters of the alphabet >> a='ab1234fg';
>> isletter(a)
ans =
1 1 0 0 0 0 1 1
isspace Returns true (1) for whitespace >> a='ab 12 34fg';
>> isspace(a)
ans =
0 0 1 1 0 0 1 0 0 0 0
isstrprop Returns true (1) for character matching the specified >> A = isstrprop('abc123def', 'digit')
property. A=
0 0 0 1 1 1 0 0 0
>> A = isstrprop('abc123def', 'alpha')
A=
1 1 1 0 0 0 1 1 1
String strcat Concatenate strings. >> a='abhgdf';
operations >> b='12345lka';
>> strcat(a,b)
ans =
abhgdf12345lka
strvcat Concatenate strings vertically. >> strvcat(a,b)
ans =
abhgdf
12345lka
strcmp Returns true (1) if two strings are identical. >> strcmp('abcd','ABCd')
ans =
0
strcmpi Returns true (1) if two strings are identical, ignoring case. >> strcmpi('abcd','ABCd')
ans =
1
strncmp Returns true (1) if first n characters of two strings are >> strncmp('abcd','abCD',3)
identical. ans =
0
strncmpi Returns true (1) if first n characters of two strings are >> strncmpi('abcd','abCD',3)
identical, ignoring case. ans =
1
findstr Find one string within another one. >> findstr('abcdefg','de')
ans =
4
strjust Justify string >> a='abcd ';
strjust(S, 'left') returns a left-justified version of S. >> strjust(a)
strjust(S, 'center') returns a center-justified version of S. ans =
abcd
>> strjust(a,'left')
ans =
abcd
>> strjust(a,'right')
ans =
abcd
strmatch Find matches for string >> x = strmatch('max', strvcat('max', 'minimax',
'maximum'))
x=
1
3
>> x = strmatch('max', strvcat('max', 'minimax',
'maximum'),'exact')
x=
1
strrep Replace one string with another. >> s1 = 'This is a good example.';
str = strrep(s1, 'good', 'great')
str =
This is a great example.
strtok Find token in string >> s = ' This is a simple example.';
[token, remain] = strtok(s)
token =
This
remain =
is a simple example.
>> s = ' Thisis a simple example.';
>> [token, remain] = strtok(s)
token =
Thisis
remain =
a simple example.
upper Convert string to uppercase >> upper('abcdEF')
ans =
ABCDEF
lower Convert string to lowercase >> lower('ABcdE')
ans =
abcde
Number-to- int2str Convert integer to string >> int2str(12079)
string ans =
conversion 12079
num2str Convert number to string >> num2str(12079)
ans =
12079
mat2str Convert matrix to string >> a=[1 2 3 7 5; 4 6 2 9 7]
a=
1 2 3 7 5
4 6 2 9 7
>> mat2str(a)
ans =
[1 2 3 7 5;4 6 2 9 7]
sprintf Write formatted data to string >> a = sprintf('%e', 12345.678)
a=
1.234568e+004
String-to- str2double Convert string to a double value. >> a='12567'
number a=
conversion 12567
>> str2double(a)/3
ans =
4189

str2num Convert string to number. >> str2double(a)/3


ans =
4189
>> str2num(a)/3
ans =
4189
sscanf Read formatted data from string >> s = '2.7183 3.1416';
A = sscanf(s,'%f')
A=
2.7183
3.1416

You might also like