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 greeting s x y 1x45 1x13 1x2 1x2 90 26 16 16 Class char array char array double array 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 s1 text are character strings 1 size s1 1 26 z Columns 1 through 12 116 101 120 116 Columns 13 through 24 114 97 99 116 Columns 25 through 26 103 115 size z 1 32 97 114 101 32 99 104 97 101 114 32 115 116 114 105 110 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 n g s b a s e 2 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 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 3 Example 10 1 6 NLteams char N Y Mets Atlanta Braves Milwaukee Brewers disp NLteams disp size NLteams size NLteams disp ALteams char Chicago White Sox Seattle Mariners Texas Rangers disp ALteams disp size ALteams size ALteams disp teams strcat NLteams ALteams disp teams disp size teams size teams disp N Y Mets Atlanta Braves Milwaukee Brewers size NLteams 3 17 Chicago White Sox Seattle Mariners Texas Rangers size ALteams 3 18 N Y Mets Chicago White Sox Atlanta Braves Seattle Mariners Milwaukee Brewers Texas Rangers size teams 3 31 When the function char is used to create string arrays with multiple rows individual rows may contain padded zeros to assure that all rows are the same length The padded zeros can be removed after accessing the individual rows 1 n string arrays by deblank ing them Example 10 1 7 schools char Central Florida Harvard Florida State disp schools disp FLschools schools 1 schools 3 disp FLschools disp other1 schools 2 size other1 size other1 disp other2 deblank schools 2 size other2 size other2 Central Florida Harvard Florida State Central Florida Florida State other1 Harvard 4 size other1 1 15 1 7 other2 Harvard size other2 5 10 2 Numbers to Strings to Numbers Often times it is desirable to convert information from one data type to another In particular it is common to transform numerical data into text strings for establishing the flow of executable commands within MATLAB s programming environment In addition numbers are sometimes converted to strings for annotation of charts and graphs The essential MATLAB functions for accomplishing data type conversions involving integer and floating point numbers are int2str num2str and sprintf Example 10 2 1 x 1 3 5 7 9 11 x is an integer array disp class x size x size x disp xs int2str x xs is a character array disp class xs size xs size xs disp xs10 xs 1 10 xs10 is a string array of the first 10 characters of xs x 1 2 3 5 6 7 9 10 11 double size x xs 1 2 1 9 3 5 6 7 9 10 11 char size xs xs10 1 2 1 33 3 A 25 5 randn 2 3 A is a floating point array disp class A size A size A disp As num2str A As is a character array disp class As size As size As As2 As 2 10 end As2 is a portion of the second row of As A 22 8372 16 6721 25 6267 26 4384 19 2676 30 9546 double size A As 22 8372 16 6721 2 3 25 6267 26 4384 19 2676 30 9546 6 char size As 2 33 As2 26 4384 30 9546 The function sprintf is similar to the ANSI C function and is used for creating character strings which can be displayed or manipulated Very often the string is comprised of text and numerical data stored in variables which is converted to character strings according to a specified format conversion Example 10 2 2 format long p pi 1000 disp p s1 sprintf 5e n p Exponential conversion and display 5 places after the decimal point p s2 sprintf 5f n p Fixed point conversion and display 5 places after decimal point p s3 sprintf 5g n p General e or f conversion and display a total of 5 digits p 3 141592653589793e 003 p s1 3 14159e 003 p s2 3141 59265 p s3 3141 6 Example 10 2 3 n 500 x rand 1 n ave mean x s sprintf The average of 0f random numbers is 3f n ave disp s disp The average of 500 random numbers is 0 507 There are ocassions when it may necessary to convert fom a character string to a number The MATLAB funtions str2num and eval are applicable Example 10 2 4 s 2500 disp class s size s size s x str2num s disp class x size x size x 7 s 2500 char size s x 1 4 1 1 2500 double size x The argument of …
View Full Document