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

Managing Variables         13


           Managing Variables


                     We have now encountered three different classes of MATLAB data: floating
                     point numbers, strings, and symbolic expressions. In a long MATLAB session
                     it may be hard to remember the names and classes of all the variables you
                     have defined. You can type whos to see a summary of the names and types of
                     your currently defined variables. Here’s the output of whos for the MATLAB
                     session displayed in this chapter:

                       >> whos
                          Name   Size        Bytes        Class
                          ans    1 x 1       226          sym object
                          u      1 x 1         8          double array
                          v      1 x 1         8          double array
                          x      1 x 1       126          sym object
                          y      1 x 1       126          sym object
                       Grand total is 58 elements using 494 bytes

                       We see that there are currently five assigned variables in our MATLAB
                     session. Three are of class “sym object”; that is, they are symbolic objects. The
                     variables x and y are symbolic because we declared them to be so using syms,
                     and ans is symbolic because it is the output of the last command we executed,
                     which involved a symbolic expression. The other two variables, u and v, are
                     of class “double array”. That means that they are arrays of double-precision
                     numbers; in this case the arrays are of size 1 × 1 (that is, scalars). The “Bytes”
                     column shows how much computer memory is allocated to each variable.
                       Try assigning u = pi, v = ’pi’, and w = sym(’pi’), and then type
                     whos to see how the different data types are described.
                       The command whos shows information about all defined variables, but it
                     does not show the values of the variables. To see the value of a variable, simply
                     type the name of the variable and press ENTER or RETURN.
                       MATLAB commands expect particular classes of data as input, and it is
                     important to know what class of data is expected by a given command; the help
                     text for a command usually indicates the class or classes of input it expects. The
                     wrong class of input usually produces an error message or unexpected output.
                     For example, type sin(’pi’) to see how unexpected output can result from
                     supplying a string to a function that isn’t designed to accept strings.
                       To clear all defined variables, type clear or clear all. You can also type,
                     for example, clear x y to clear only x and y.
                       You should generally clear variables before starting a new calculation.
                     Otherwise values from a previous calculation can creep into the new
   27   28   29   30   31   32   33   34   35   36   37