Text functions
char vs. string Matlab has a new string class, that you are discouraged from using for this course. You can create a string object using double-quotes. e.g., s="apple" We will only discuss char vectors. You can create a char vector using single-quotes. e.g. s='apple'
ASCII Table
Matlab Text: one character ~ one number >> double('hello') ans = 104 101 108 108 111 >> %newline is not a new row in the matrix >> char([104 101 108 10 108 111 ]) hel lo >> %two '' encodes a single ' in a string >> msg='ahmet''s presentation' msg = ahmet's presentation
Text functions >> ischar('Hello 123') 1 >> lower('Hello 123') hello 123 >> upper('Hello 123') HELLO 123 >> isletter('Hello 123') 1 1 1 1 1 0 0 0 0 >> isspace('Hello 123') 0 0 0 0 0 1 0 0 0
Multiple texts Use cell arrays when you need to store multiple texts in a variable. You would typically need to a vector of texts, but a matrix of texts (e.g., when you load data from an Excel file) is also possible. >> msgs={'hello','yes','no'} msgs = 'hello' 'yes' 'no’ >> msgs={'hello','world';'yes','no'} 'hello' 'world' 'yes' 'no'
strjoin(): Join Multiple Texts >> % join using a space character >> strjoin( {'apple', 'orange', 'cherry'} ) ans = 'apple orange cherry' >> % join with a custom character/text >> strjoin( {'apple', 'orange', 'cherry'}, ',') 'apple,orange,cherry' >> strjoin( {'apple', 'orange', 'cherry'}, ' and ') 'apple and orange and cherry'
Concatenating Texts >> % Using strjoin() >> names = {'apple', 'orange', 'cherry'} ; >> name=strjoin(names, '') name = 'appleorangecherry' >> % Using vector notation: >> name=[names{1}, names{2}, names{3}] >> name=[ names{1:end} ] >> %Using sprintf() >> name = sprintf( '%s%s', 'apple', 'orange') 'appleorange'
strsplit(): Splitting text into multiple texts strsplit(s,delimiter) strsplit(s,{delimiter1, delimiter2}) strsplit(... ,'CollapseDelimiters',false) >> strsplit('apple,,,orange,cherry', ',') ans = 1×3 cell array {'apple'} {'orange'} {'cherry'} >> strsplit('apple,,,orange,cherry', ',','CollapseDelimiter',false) 1×5 cell array {'apple'} {0×0 char} {0×0 char} {'orange'} {'cherry'}
strtrim(): Trimming blank spaces >> [ '#' strtrim(' ahmet sacan ') '#' ] ans = #ahmet sacan#
strcmp(): Compare Texts strcmp('str1','str2') strcmpi('str1','Str2') strncmp('str1', 'str2', n) strncmpi('str1', 'Str2', n) >> [strcmp('yes','yes'), strcmp('yes','no')] >> strcmp('yes','YES') >> strcmpi('yes','YES') >> strcmp(lower('yes'),lower('YES')) >> strcmp('yes','yes ') >> strcmp('yes’, strtrim('yes '))
strcmp(): Compare with multiple texts strcmp( {'str1','str2'}, 'str2') find(strcmp( {'str1','str2'}, 'str2'))
strfind(): Locate Subtext k=strfind(haystack, needle) >> strfind('Find the starting indices of the pattern string', 'in') 2 15 19 45
Replacing Subtext strrep(haystack, oldneedle, newneedle) >> strrep('flow crow','ow','y')
Conversion from number to text >> sprintf('%.2f %.0f', 7/3, 7/3) ans = '2.33 2'
Conversion to number from text x = str2double( str ) >> str2double( '123.45e7' ) >> str2double( '1,200' )