
Other Data Structures
joins the strings horizontally and produces
h=
Hello world
The statement
v = [s; 'world']
joins the strings vertically and produces
v=
Hello
world
Notethatablankhastobeinsertedbeforethe'w' in h and that both words
in
v have to have the same length. The resulting arrays are both character
arrays;
h is 1-by-11 and v is 2-by-5.
To m anipulate a body of text containing lines of different lengths, you have
two choices—a padded character array or a cell array of strings. When
creating a character array, you must make each row of the array the same
length. (Pad the ends of the shorter rows with spaces.) The
char function do es
this padding for you. For example,
S = char('A','rolling ','stone','gathers', 'momentum.')
produces a 5-by-9 character array:
S=
A
rolling
stone
gathers
momentum.
Alternatively, you can store the text in a cell array. For example,
C = {'A';'rolling';'s tone';'gathers';'mom entum.'}
creates a 5-by-1 cell array that requires no padding because each row of the
array can have a different length:
4-15
Comentarios a estos manuales