Variable Assignment
The equality sign is used to assign values to variables:
>> x = 3
x =
3
>> y = x^2
y =
9
Variables in MATLAB are case sensitive. Hence, the variables "x" and "y" are distinct from "X" and "Y" (at this point, the latter are in fact, undefined).
Output can be suppressed by appending a semicolon to the command lines.
>> x = 3;
>> y = x^2;
>> y
y =
9
Active Variables
At any time you want to know the active variables you can use who:
>> who Your variables are: ans x y
Removing a Variable
To remove a variable, try
this: >> clear x
To remove all the variables from workspace, use clear >> clear
Saving and Restoring Variables
To save the value of the variable "x" to a plain text file named "x.value"
use >> save x.value x ‐ascii
To save all variables in a file named mysession.mat, in reloadable format, use
>> save mysession To restore the session, use
>> load mysession
VARIABLE ARITHMETIC
MATLAB uses some fairly standard notation. More than one command may be entered on a single line, if they are separated by commas.
>> 2+3;
>> 3*4, 4^2;
Powers are performed before division and multiplication, which are done before subtraction and addition. For example
>> 2+3*4^2;
generates ans = 50. That is:
2+3*4^2 ==> 2 + 3*4^2 <== exponent has the highest precedence ==> 2 + 3*16 <== then multiplication operator
==> 2 + 48 <== then addition operator
==> 50
Dostları ilə paylaş: |