MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Create Character Arrays


Create Character Vector


Create a character vector by enclosing a sequence of characters in single quotation
marks.

chr = 'Hello, world'

chr =

'Hello, world'

Character vectors are 1 -by-n arrays of type char. In computer programming, string is a
frequently-used term for a 1 -by-n array of characters. However, starting in R2016b
MATLAB also provides a string data type, so 1 -by-n character arrays are referred to as
character vectors in MATLAB documentation.

whos chr

Name Size Bytes Class Attributes

chr 1x12 24 char

If the text contains a single quotation mark, include two quotation marks when assigning
the character vector.

newChr = 'You''re right'

newChr =

'You're right'

Functions such as uint16 convert characters to their numeric codes.

chrNumeric = uint16(chr)

chrNumeric =

1×12 uint16 row vector

72 101 108 108 111 44 32 119 111 114 108 100

6 Characters and Strings

Free download pdf