In this tutorial, we will learn how to declare and use the variables in MATLAB command window prompt. Variables are used to store information. We can use the assignment operator (=) to store information.
Let’s use some variables and assign values to the them in the Command Window prompt.
Calculation using the variables.
>> a = 7; >> b = 9; >> ans = a*b ans = 63
Variables a and b are assigned values. ‘a’ and ‘b’ are initialized to 7 and 9 values respectively. The product of and b is stored in ‘ans’ variable.
a = 7;
The statement declared the variable and stores the value 7 in the variable. We can reassign the variables if you want so.
The variable names and the current values will appear in the Current Workspace window.
There are some rules for naming the variables in MATLAB.
- The first letter of the variable name should be a letter.
- Valid characters are letters and numbers.
- Special characters are not allowed except underscore( _ ) .
- Spaces are not allowed in variable names.
- Variable names are case sensitive.
If you use invalid variable names, MATLAB will throw an error message and a suggested correction. You can use this command if you want, or hit Esc button to delete the suggestion.
Did you mean:
>> invalid$ = 10; invalid$ = 10; ↑ Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters. >> 10name = 'Hello'; 10name = 'Hello'; ↑ Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses. Did you mean: >> name = 'Hello';
>> radius = 5;
>> area = pi*radius^2
pi is the floating point number and the value of . It is the ratio of circle’s circumference to the diameter.