Page 87 - Basics of MATLAB and Beyond
P. 87

str  =
                               you’re the one
                               >> str = ’’’you’’re the one’’’
                               str  =
                               ’you’re the one’
                                    Exercise 8 Create a table of integers from 0 to 255 and their
                                    equivalent ascii values. Printing which ascii “character” rings
                                    the system bell? (Answer on page 187.)



                               26.1   String Matrices
                               To create a matrix of strings, use the semicolon to separate the lines:

                               >> m = [alph ; char(num+5) ; ’KLMNO’]
                               m  =
                               ABCDE
                               FGHIJ
                               KLMNO

                               You cannot create a matrix of strings having different lengths:

                               >> z = [alph ; ’b’]
                               ???  All rows in the bracketed expression must have the
                               same number of columns.
                               (You should use cell arrays—discussed later—if you really want to create
                               a “matrix” like this.) To simulate the effect, though, you can pad with
                               zeros:

                               >> z = [’abcd’ ; ’b    ’]
                               z  =
                               abcd
                               b

                               The second line has three blank spaces to the right of the “b”. A conve-
                               nient way to do this is to use the char function, which does the padding
                               for you:

                               >> z = char(’These’,’lines are’,’of varying lengths.’)
                               z  =
                               These
                               lines are
                               of varying lengths.






                               c   2000 by CRC Press LLC
   82   83   84   85   86   87   88   89   90   91   92