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.

2 Data Types<br />

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

'; 'Senior Developer'];<br />

A simpler way to create string arrays is to use the char function. char<br />

automatically pads all strings to the length of the longest input string. In the<br />

following example, char pads the 13-character input string 'Thomas R. Lee'<br />

with three trailing blanks so that it will be as long as the second string:<br />

name = char('Thomas R. Lee','Senior Developer')<br />

name =<br />

Thomas R. Lee<br />

Senior Developer<br />

When extracting strings from an array, use the deblank function to remove<br />

any trailing blanks:<br />

trimname = deblank(name(1,:))<br />

trimname =<br />

Thomas R. Lee<br />

size(trimname)<br />

ans =<br />

1 13<br />

Expanding Character Arrays<br />

Expandingthesizeofanexistingcharacterarraybyassigningadditional<br />

characters to indices beyond the bounds of the array such that part of the<br />

array becomes padded with zeros, is generally not recommended. See the<br />

documentation on “Expanding a Character Array” on page 1-31 in the<br />

<strong>MATLAB</strong> <strong>Programming</strong> documentation.<br />

Cell Arrays of Strings<br />

Creating strings in a regular <strong>MATLAB</strong> array requires that all strings in the<br />

array be of the same length. This often means that you have to pad blanks at<br />

the end of strings to equalize their length. However, another type of <strong>MATLAB</strong><br />

array, the cell array, can hold different sizes and types of data in an array<br />

without padding. Cell arrays provide a more flexible way to store strings of<br />

varying length.<br />

2-40

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

Saved successfully!

Ooh no, something went wrong!