02.03.2016 Views

MATLAB by rudra pratap

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.3 Character strings 79<br />

howdy = [' Hiuuuuu ' ; 'Hellouu ' ; 'Namaste']<br />

with each string being seven characters long (u denotes a space) .<br />

An easier way of doing the same thing is to use the command char, which<br />

converts strings to a matrix. char (s1, s2, . . .) puts each string argument s1, s2 ,<br />

etc., in a row and creates a string matrix <strong>by</strong> padding Pach row with the appropriate<br />

number of blanks. Thus , to create the same howdy as previously, we type<br />

howdy = char('Hi', 'Hello' , 'Namaste ').<br />

You can also use strvcat functions to create string arrays from string arguments.<br />

However, char is the best function for this purpose.<br />

Because the same quote character (the single right quote) is used to signal the<br />

beginning as well as the end of a string, this character cannot be used inside a<br />

string for a quote or apostrophe. For a quote within a string you must use two<br />

single quotes, ''. Thus, to title a graph with 3-D View of Boomerang 's Path, you<br />

write title ('3-D View of Boomerang ' ' s Path').<br />

3.3.1 Manipulating character strings<br />

Character strings can be manipulated just like matrices. Thus,<br />

c = [howdy(2,:) names (3 , : )]<br />

produces Hello Mary as the output for c. This feature can be used along with<br />

number-to-string conversion functions, such as num2str and int2str , to create text<br />

objects containing dynamic values of variables. Such text objects are particularly<br />

useful in creating titles for figures and other graphics annotation commands (see<br />

Chapter 6). For example, suppose you want to produce a few variance-study graphs<br />

with different values of the sample size n, an integer variable. Producing the title<br />

of the graph with the command<br />

title(['Variance study with sample size n = ',int2str(n) ])<br />

writes titles with the current value of n printed in the title.<br />

There are several built-in functions for character string manipulation:<br />

char<br />

abs<br />

blanks(n)<br />

de blank<br />

eval<br />

findstr<br />

int2str<br />

is char<br />

isletter<br />

lower<br />

mat2str<br />

num2str<br />

strcmp<br />

strncmp<br />

strcat<br />

strvcat<br />

upper<br />

creates character arrays using automatic padding,<br />

also, converts ASCII numeric values to character arrays,<br />

converts characters to their ASCII numeric values,<br />

creates n blank characters,<br />

removes the trailing blanks from a string,<br />

executes the string as a command (see Section 3.3.2) ,<br />

finds the specified substring in a given string,<br />

converts integers to strings (see example given earleir) ,<br />

true ( = 1) for character arrays,<br />

true ( = 1) for alphabetical characters,<br />

converts any uppercase letters in the string to lowercase,<br />

converts a matrix to a string,<br />

converts numbers to strings (similar to int2str) ,<br />

compares two strings, returns 1 if same,<br />

compares the first n characters in given strings,<br />

concatenates strings horizontally, ignoring trailing blanks,<br />

concatenates strings vertically, ignoring empty strings, and<br />

converts any lowercase letters in the string to uppercase.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!