Presentation is loading. Please wait.

Presentation is loading. Please wait.

Text functions.

Similar presentations


Presentation on theme: "Text functions."— Presentation transcript:

1 Text functions

2 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'

3 ASCII Table

4 Matlab Text: one character ~ one number
>> double('hello') ans = >> %newline is not a new row in the matrix >> char([ ]) hel lo >> %two '' encodes a single ' in a string >> msg='ahmet''s presentation' msg = ahmet's presentation

5 Text functions >> ischar('Hello 123') 1 >> lower('Hello 123') hello 123 >> upper('Hello 123') HELLO 123 >> isletter('Hello 123') >> isspace('Hello 123')

6 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'

7 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'

8 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'

9 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'}

10 strtrim(): Trimming blank spaces
>> [ '#' strtrim(' ahmet sacan ') '#' ] ans = #ahmet sacan#

11 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 '))

12 strcmp(): Compare with multiple texts
strcmp( {'str1','str2'}, 'str2') find(strcmp( {'str1','str2'}, 'str2'))

13 strfind(): Locate Subtext
k=strfind(haystack, needle) >> strfind('Find the starting indices of the pattern string', 'in')

14 Replacing Subtext strrep(haystack, oldneedle, newneedle)
>> strrep('flow crow','ow','y')

15 Conversion from number to text
>> sprintf('%.2f %.0f', 7/3, 7/3) ans = '2.33 2'

16 Conversion to number from text
x = str2double( str ) >> str2double( '123.45e7' ) >> str2double( '1,200' )


Download ppt "Text functions."

Similar presentations


Ads by Google