23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

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

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

Strings<br />

Store Arrays of Strings in a Cell Array<br />

It is usually best to store an array of strings in a cell array instead of a<br />

character array, especially if the strings are of different lengths. Strings in<br />

a character array must be of equal length, which often requires padding the<br />

strings with blanks. This is not necessary when using a cell array of strings<br />

that has no such requirement.<br />

The cellRecord below does not require padding the strings with spaces:<br />

cellRecord = {'Allison Jones'; 'Development'; 'Phoenix'};<br />

For more information: See “Cell Arrays of Strings” on page 2-40 in the<br />

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

Converting Between Strings and Cell Arrays<br />

You can convert between standard character arrays and cell arrays of strings<br />

using the cellstr and char functions:<br />

charRecord = ['Allison Jones'; 'Development '; ...<br />

'Phoenix '];<br />

cellRecord = cellstr(charRecord);<br />

Also, a number of the <strong>MATLAB</strong> string operations can be used with either<br />

character arrays, or cell arrays, or both:<br />

cellRecord2 = {'Brian Lewis'; 'Development'; 'Albuquerque'};<br />

strcmp(charRecord, cellRecord2)<br />

ans =<br />

0<br />

1<br />

0<br />

For more information: See “Converting to a Cell Array of Strings” on page<br />

2-41 and “String Comparisons” on page 2-56 in the documentation.<br />

Search and Replace Using Regular Expressions<br />

Using regular expressions in <strong>MATLAB</strong> offers a very versatile way of searching<br />

for and replacing characters or phrases within a string. See the help on these<br />

functions for more information.<br />

12-31

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

Saved successfully!

Ooh no, something went wrong!