MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

The char function converts the integer vector back to characters.


chrAlpha = char([72 101 108 108 111 44 32 119 111 114 108 100])


chrAlpha =


'Hello, world'


Create Rectangular Character Array


Character arrays are m-by-n arrays of characters, where m is not always 1. You can join
two or more character vectors together to create a character array. This is called
concatenation and is explained for numeric arrays in “Creating, Concatenating, and
Expanding Matrices”. As with numeric arrays, you can combine character arrays
vertically or horizontally to create a new character array.


However, it is recommended that you store character vectors in a cell array on page 6-
21, instead of using m-by-n character arrays. Cell arrays are flexible containers that
allow you to easily store character vectors of varying length.


Combine Character Vectors Vertically


To combine character vectors into a two-dimensional character array, use square brackets
or the char function.



  • Apply the MATLAB concatenation operator, []. Separate each row with a semicolon
    (;). Each row must contain the same number of characters. For example, combine
    three character vectors of equal length:


devTitle = ['Thomas R. Lee'; ...
'Sr. Developer'; ...
'SFTware Corp.']

devTitle =

3×13 char array

'Thomas R. Lee'
'Sr. Developer'
'SFTware Corp.'

If the character vectors have different lengths, pad with space characters as needed.
For example:

Create Character Arrays
Free download pdf