Variables in MATLAB represents the named storage locations , whose values can be manipulated during program run .
Why to use Variables in MATLAB ?
After you have assigned Variables in MATLAB you can use them anywhere in the program by simply using these assigned names it becomes essential when you have to use some value or a big expression repeatedly in your program , so after defining the variable name you can use them use them anywhere in your program depending upon global or local space .
>> x = 6 x is variable here which stores the value 6 in it .
There are few rules for defining the variables names .
Rules for defining Variables in MATLAB
1) The name of the Variables in MATLAB must start with a letter it can’t be any other character, after the first character (letter) it can contain any letters, digit, underscore, character except the space .
>> x_1 = 6; >> x23 = 7; >> xyz = 8 >> x 1 = 6; Undefined function or variable 'x'. >> 23x = 6; 23x = 6; ↑ Error: Unexpected MATLAB expression. i.e x_1,x23,xyz are valid variable names while x 1,23x are invalid variable name
2) Certain reserved words can’t be used as variable names that convey different meaning to MATLAB internally .
>> if = 36 if = 36 ↑ Error: The expression to the left of the equals sign is not a valid target for an assignment. >> else = 23 ; else = 23 ; ↑ Error: Illegal use of reserved keyword "else".
3) Variables in MATLAB are case-sensitive . which means the uppercase-letter and the lowercase-letter are different . i.e x and X are different.
Creating Variables in MATLAB
1) Variables in MATLAB can be created simply by following the command
<variable_name>= expression
*variable name must always be on the left side of the assignment operator and a valid name .
>> x=6 x = 6 >>z=3*6-2; >> y = input('enter the value of y'); enter the value of y 6 putting a semicolon at the end suppresses the output . variable z and y will be internally stored while value of x will be displayed and stored finally.
2. If no variable name is assigned to the expression then MATLAB by itself gives it a name ‘ans’ .
>> 1+1 ans = 2
Note – Values of the Variables in MATLAB can be changed or altered at any instant of the program .
i.e x=x+1 increments the value of x by 1 , x=x-1 decrements the value by 1
You can have a look at your previously created variables at the work space window on the left side or by simply typing who or whos in command window .
>>x= 1 x = 1 >>1+1 ans = 2 >>y=13 y = 13 >> who your variables are x ans y >>whos Name Size Bytes Class Attributes x 1x1 8 double ans 1x1 8 double y 1x1 8 double whos give more information about the variables as compared to who
You can also delete the variables by using clear command that will clear all the previous variables or by either using clear <variable_name> that deletes the specific variable .
>>x=6; >>y=7; >>z=8; >>x = 6 >>clear<x> >>x Undefined function or variable x >>y = 7 >>clear >>y Undefined function or variable y >>z Undefined function or variable z
Hope you are now aware about the variables of MATLAB and rules to create MATLAB variable.