MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

2 Data Types name = ['Thomas R. Lee '; 'Senior Developer']; A simpler way to create string arrays is to use the char function. char automatically pads all strings to the length of the longest input string. In the following example, char pads the 13-character input string 'Thomas R. Lee' with three trailing blanks so that it will be as long as the second string: name = char('Thomas R. Lee','Senior Developer') name = Thomas R. Lee Senior Developer When extracting strings from an array, use the deblank function to remove any trailing blanks: trimname = deblank(name(1,:)) trimname = Thomas R. Lee size(trimname) ans = 1 13 Expanding Character Arrays Expandingthesizeofanexistingcharacterarraybyassigningadditional characters to indices beyond the bounds of the array such that part of the array becomes padded with zeros, is generally not recommended. See the documentation on “Expanding a Character Array” on page 1-31 in the MATLAB Programming documentation. Cell Arrays of Strings Creating strings in a regular MATLAB array requires that all strings in the array be of the same length. This often means that you have to pad blanks at the end of strings to equalize their length. However, another type of MATLAB array, the cell array, can hold different sizes and types of data in an array without padding. Cell arrays provide a more flexible way to store strings of varying length. 2-40

Characters and Strings For details on cell arrays, see “Cell Arrays” on page 2-94. Converting to a Cell Array of Strings The cellstr function converts a character array into a cell array of strings. Consider the character array data = ['Allison Jones';'Development ';'Phoenix ']; Each row of the matrix is padded so that all have equal length (in this case, 13 characters). Now use cellstr to create a column vector of cells, each cell containing one of the strings from the data array: celldata = cellstr(data) celldata = 'Allison Jones' 'Development' 'Phoenix' Note that the cellstr function strips off the blanks that pad the rows of the input string matrix: length(celldata{3}) ans = 7 The iscellstr function determines if the input argument is a cell array of strings. It returns a logical 1 (true) inthecaseofcelldata: iscellstr(celldata) ans = 1 Use char to convert back to a standard padded character array: strings = char(celldata) strings = Allison Jones Development 2-41

Characters and Strings<br />

For details on cell arrays, see “Cell Arrays” on page 2-94.<br />

Converting to a Cell Array of Strings<br />

The cellstr function converts a character array into a cell array of strings.<br />

Consider the character array<br />

data = ['Allison Jones';'Development ';'Phoenix '];<br />

Each row of the matrix is padded so that all have equal length (in this case,<br />

13 characters).<br />

Now use cellstr to create a column vector of cells, each cell containing one<br />

of the strings from the data array:<br />

celldata = cellstr(data)<br />

celldata =<br />

'Allison Jones'<br />

'Development'<br />

'Phoenix'<br />

Note that the cellstr function strips off the blanks that pad the rows of the<br />

input string matrix:<br />

length(celldata{3})<br />

ans =<br />

7<br />

The iscellstr function determines if the input argument is a cell array of<br />

strings. It returns a logical 1 (true) inthecaseofcelldata:<br />

iscellstr(celldata)<br />

ans =<br />

1<br />

Use char to convert back to a standard padded character array:<br />

strings = char(celldata)<br />

strings =<br />

Allison Jones<br />

Development<br />

2-41

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

Saved successfully!

Ooh no, something went wrong!