MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 90 and 91: 1 Data Structures Computing with Mu
- Page 92 and 93: 1 Data Structures • As multivaria
- Page 94 and 95: 1 Data Structures A{2,2} = 7; B{1,1
- Page 96 and 97: 1 Data Structures Summary of Matrix
- Page 98 and 99: 1 Data Structures Functions to Dete
- Page 100 and 101: 1 Data Structures 1-76
- Page 102 and 103: 2 Data Types Cell Arrays (p. 2-94)
- Page 104 and 105: 2 Data Types Data Type Summary The
- Page 106 and 107: 2 Data Types Numeric Types Numeric
- Page 108 and 109: 2 Data Types the two equally nearby
- Page 110 and 111: 2 Data Types x = x = 127 -128 Also,
- Page 112 and 113: 2 Data Types Set the warning state
- Page 114 and 115: 2 Data Types Floating-Point Numbers
- Page 116 and 117: 2 Data Types isfloat(x) ans = 1 You
- Page 118 and 119: 2 Data Types In this example, 7.5 d
- Page 120 and 121: 2 Data Types 8.881784197001252e-016
- Page 122 and 123: 2 Data Types Note that the order of
- Page 124 and 125: 2 Data Types Complex Numbers Comple
- Page 126 and 127: 2 Data Types NaN MATLAB represents
- Page 128 and 129: 2 Data Types All available formats
- Page 130 and 131: 2 Data Types Integer Functions Func
- Page 132 and 133: 2 Data Types Infinity and NaN Funct
- Page 134 and 135: 2 Data Types 1 0 0 1 0 1 1 0 The MA
- Page 136 and 137: 2 Data Types A(B) ans = 5 15 30 35
- Page 138 and 139: 2 Data Types Characters and Strings
- Page 142 and 143: 2 Data Types Phoenix length(strings
- Page 144 and 145: 2 Data Types • “Setting Field W
- Page 146 and 147: 2 Data Types Ordered Sequentially s
- Page 148 and 149: 2 Data Types An alternate syntax, t
- Page 150 and 151: 2 Data Types Precision is not usual
- Page 152 and 153: 2 Data Types Note You can specify m
- Page 154 and 155: 2 Data Types operator, specifying t
- Page 156 and 157: 2 Data Types Valid Syntax Invalid S
- Page 158 and 159: 2 Data Types A = 'fate'; B = 'cake'
- Page 160 and 161: 2 Data Types while (any(remainder))
- Page 162 and 163: 2 Data Types Converting from String
- Page 164 and 165: 2 Data Types Function Summary MATLA
- Page 166 and 167: 2 Data Types Functions to Determine
- Page 168 and 169: 2 Data Types Date Strings There are
- Page 170 and 171: 2 Data Types • The day field is a
- Page 172 and 173: 2 Data Types datestr(d) ans = 01-Ma
- Page 174 and 175: 2 Data Types Utility Functions (Con
- Page 176 and 177: 2 Data Types • “Applying Functi
- Page 178 and 179: 2 Data Types where the arguments ar
- Page 180 and 181: 2 Data Types You can access subarra
- Page 182 and 183: 2 Data Types Dynamic Field Names Ex
- Page 184 and 185: 2 Data Types Deleting Fields from S
- Page 186 and 187: 2 Data Types plot(mercury, 'b') plo
- Page 188 and 189: 2 Data Types There are at least two
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