Often, a result of some calculation is needed for later use, or perhaps a complicated expression can be examined more carefully if done in parts. Both can be done by the use of "variables". Variables hold whatever result you put in them by the use of the equal sign (
x=1creates a variable called "x" and stores the value "1" in it. If one then types "x" in an expression, MATLAB® will use the value stored in "x", i.e., "1".
x=2now the variable x "contains" the value "2".
xas part of an expression:
A= [1 2 3 4]
In this last example, we are getting ahead of ourselves by referring to an element of a vector. We will touch on that more later.
Note that you can "hide" important MATLAB functions and constants by defining a variable with the same name:
pi=3 will give interesting results later (to remove
clear pi). This is usually not a good idea, so take care before using a nice name like
det, as these are all built-in functions. You can check if a variable is already in use by using the
>> which pi built-in (/Applications/MATLAB_R2011b.app/toolbox/MATLAB/elmat/pi)
tells us that
pi is a built-in function, while
>> which Pi 'Pi' not found.
tells us that
Pi is unused. The difference is in the capitalization. MATLAB-defined functions will always use lower-case names (even if the helpfile will show these as all CAPITAL), which implies that you can always avoid collision by capitalizing the fiirst letter of your variable and functions names.
y=2. Exchange the values of these two variables without specifically using '1' or '2' i.e., the exchange should work regardless of the values held by the variables. Hint: You can invent a new variable. Another Hint: Imagine you have misplaced your kids' breakfast and now Tom's Cornflakes are in Sally's bowl and Sally's CocoPuffs are in Tom's bowl. You have already poured the milk, how can you fix the problem without throwing away and starting over?