23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Characters and Strings<br />

name 1x13 26 char<br />

You can see that each character uses 2 bytes of storage internally.<br />

The class and ischar functions show that name is a character array:<br />

class(name)<br />

ans =<br />

char<br />

ischar(name)<br />

ans =<br />

1<br />

You also can join two or more character arrays together to create a new<br />

character array. To do this, use either the string concatenation function,<br />

strcat, or the <strong>MATLAB</strong> concatenation operator, []. The latter preserves any<br />

trailing spaces found in the input arrays:<br />

name = 'Thomas R. Lee';<br />

title = ' Sr. Developer';<br />

strcat(name,',',title)<br />

ans =<br />

Thomas R. Lee, Sr. Developer<br />

To concatenate strings vertically, use strvcat.<br />

Creating Two-Dimensional Character Arrays<br />

When creating a two-dimensional character array, be sure that each row<br />

has the same length. For example, this line is legal because both input rows<br />

have exactly 13 characters:<br />

name = ['Thomas R. Lee' ; 'Sr. Developer']<br />

name =<br />

Thomas R. Lee<br />

Sr. Developer<br />

When creating character arrays from strings of different lengths, you can pad<br />

the shorter strings with blanks to force rows of equal length:<br />

2-39

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

Saved successfully!

Ooh no, something went wrong!