Page 72 - A Guide to MATLAB for Beginners and Experienced Users
P. 72

Data Classes        53


                       Sometimes you need to convert one data class into another to prepare the
                     output of one command to serve as the input for another. For example, to use
                     plot on a symbolic expression obtained from solve, it is convenient to use
                     first vectorize and then inline, because inline does not allow symbolic
                     input and vectorize converts symbolic expressions to strings. You can make
                     the same conversion without vectorizing the expression using char. Other
                     useful conversion commands we have encountered are double (symbolic to
                     numerical), sym (numerical or string to symbolic), and inline itself (string to
                     inline function). Also, the commands num2str and str2num convert between
                     numbers and strings.


           String Manipulation

                     Often it is useful to concatenate two or more strings together. The simplest way
                     to do this is to use MATLAB’s vector notation, keeping in mind that a string is
                     a “row vector” of characters. For example, typing [string1, string2] com-
                     bines string1 and string2 into one string.
                       Here is a useful application of string concatenation. You may need to define
                     a string variable containing an expression that takes more than one line to
                     type. (In most circumstances you can continue your MATLAB input onto the
                     next line by typing ... followed by ENTER or RETURN, but this is not allowed
                     in the middle of a string.) The solution is to break the expression into smaller
                     parts and concatenate them, as in:
                       >> eqn = [’left hand side of equation = ’, ...
                       ’right hand side of equation’]

                       eqn =
                       left hand side of equation = right hand side of equation


           Symbolicand Floating Point Numbers

                     We mentioned above that you can convert between symbolic numbers and
                     floating point numbers with double and sym. Numbers that you type are,
                     by default, floating point. However, if you mix symbolic and floating point
                     numbers in an arithmetic expression, the floating point numbers are auto-
                     matically converted to symbolic. This explains why you can type syms x and
                     then xˆ2 without having to convert 2 to a symbolic number. Here is another
                     example:

                       >>a=1
   67   68   69   70   71   72   73   74   75   76   77