Character Strings - Chapter 10 Character Strings 10.1...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 10 Character Strings 10.1 String Construction In MATLAB, text is stored as character strings, or strings for short. Strings are numerical arrays of ascii values which are displayed as the equivalent characters. Strings are created by enclosing text within single quotes. Example 10.1.1 clear s=' Hello there.' x=size(s) greeting='Welcome to the University of Central Florida!' y=size(greeting) whos s = Hello there. x = 1 13 greeting = Welcome to the University of Central Florida! y = 1 45 Name Size Bytes Class greeting 1x45 90 char array s 1x13 26 char array x 1x2 16 double array y 1x2 16 double array Grand total is 62 elements using 148 bytes The elements of a string array are the individual characters, each of which requires 2 bytes of storage compared to 8 bytes for elements in numerical, i.e. double arrays. The ascii representation of a string is obtained by making it the argument of the function 'double' . The functon 'char' restores the character string. Example 10.1.2 s1='text are character strings' size_s1=size(s1) z=double(s1) % Convert string to ascii representation size_z=size(z) s2=char(z) % Convert numerical array of ascii values back to string s3=char(100:107) % Convert ascii values 100-107 to characters 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
s1 = text are character strings size_s1 = 1 26 z = Columns 1 through 12 116 101 120 116 32 97 114 101 32 99 104 97 Columns 13 through 24 114 97 99 116 101 114 32 115 116 114 105 110 Columns 25 through 26 103 115 size_z = 1 26 s2 = text are character strings s3 = defghijk Strings can be addressed and manipulated the same as arrays. Strings are also concatenated the same as arrays. Example 10.1.3 s1='Greetings sport fans.' word2=s1(11:15) s2='baseball' s3=['Greetings ' s2 ' fans.'] s4=s3' s1 = Greetings sport fans. word2 = sport s2 = baseball s3 = Greetings baseball fans. s4 = G r e e t i 2
Background image of page 2
n g s b a s e b a l l f a n s . To display a string without printing its name, the function ' disp( ) ' with the string variable name as argument is used. Example 10.1.4 s1='Joe said ''Have a nice day'' ' disp(' ') disp(s1) disp(' ') disp('Happy Birthday') s1 = Joe said 'Have a nice day' Joe said 'Have a nice day' Happy Birthday Character strings arrays can have more than one row provided each row is a string with the same number of elements (characters). Example 10.1.5 list=['Harold Klee';'Chris Bauer';'Joe Smith ']; % Two blanks are added to the end of Joe Smith to make it 1 11, % the same size as strings Harold Klee and Chris Bauer disp(list) size_list=size(list) Harold Klee Chris Bauer Joe Smith size_list = 3 11 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The MATLAB function 'char( )' is also used to create multiple row string arrays from individual strings of different lengths. The function 'strcat( )' is used to horizontally concatenate string arrays with the same number of rows. Example 10.1.6
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2011 for the course EGM 4320 taught by Professor Klee during the Spring '11 term at University of Central Florida.

Page1 / 17

Character Strings - Chapter 10 Character Strings 10.1...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online