If you're already familiar with the basic setup and usage of MATLAB, check out some examples in the article below by Mytour to understand how to declare variables in MATLAB.
Variable Declaration in MATLAB
Examples of Variable Declaration in MATLAB
- Example 1:
x = 3 % define x and initialize it with a value
MATLAB will execute the above command and return the result below:
x = 3
MATLAB will create a 1-by-1 matrix named x and store the value 3 in its element.
- Example 2:
x = sqrt(16)% define x and initialize it with an expression
MATLAB will execute the command above and return the result below:
x = 4
Note:
- Once the variable is entered into the system, you can reference the variable.
- Variables must have values before being utilized.
- When an expression yields a result not assigned to any variable, the system will assign it to a variable named ans.
- Example 3:
sqrt(78)
MATLAB will execute the command above and return the result below:
ans = 8.8318
Additionally, you can use the ans variable:
sqrt(78);
9876/ans
MATLAB will execute the command above and return the result below:
ans = 1118.2
- Example 4:
x = 7 * 8;
y = x * 7.89
MATLAB will execute the command above and return the result below:
y = 441.84
Multiple Variable Assignment in MATLAB
You can assign multiple variables in MATLAB with a single command. For example:
a = 2; b = 7; c = a * b
MATLAB will execute the command above and return the result below:
c = 14
Who Command in MATLAB
If you forget a variable, you can use the command below:
Who
MATLAB will execute the command above and return the result below:
Your variables are:
a ans b c
The whos command displays fewer variables:
- Current variables in memory.
- Type of each variable.
- Memory allocated for each variable.
- Whether the variable is complex or not.
whos
MATLAB will execute the command above and return the result below:
The clear command will remove all variables from memory:
Assigning Long Variables in MATLAB
To assign long variables, you can use (...). For example:
MATLAB will execute the command above and return the result below:
final_velocity = 196
Format Command in MATLAB
By default, MATLAB displays numbers with 4 significant decimal digits. This format is called short format.
However, for more precision, you need to use the format command.
The format long command displays 16 decimal digits.
- Example 1:
format long
x = 7 + 10/3 + 5 ^ 1.2
MATLAB will execute the command above and return the result below:
x = 17.2319816406394
- Example 2:
format short
x = 7 + 10/3 + 5 ^ 1.2
MATLAB will execute the command above and return the result below:
x = 17.232
The format bank command rounds numbers to 2 decimal places.
- Example:
format bank
daily_wage = 177.45;
weekly_wage = daily_wage * 6
MATLAB will execute the command above and return the result below:
weekly_wage = 1064.70
MATLAB represents large numbers using exponential notation.
The format short e command enables display in scientific notation with 4 decimal places plus exponent.
- Example:
format short e
4.678 * 4.9
MATLAB will execute the command above and return the result below:
ans = 2.2922e+01
The format long e command allows display in scientific notation with 4 decimal places plus exponent.
- Example:
format long e
x = pi
MATLAB will execute the command above and return the result below:
x = 3.141592653589793e+00
The format rat command returns results from relational expressions.
- Example:
format rat
4.678 * 4.9
MATLAB will execute the command above and return the result below:
ans = 34177/1491
Create Vector in MATLAB
In MATLAB, a vector is defined as a one-dimensional array. MATLAB allows creating two types of vectors:
- Row vector.
- Column vector.
Row vectors are created by enclosing a set of elements in square brackets, using spaces or commas to separate elements.
- Example 1:
r = [7 8 9 10 11]
MATLAB will execute the command above and return the result below:
- Example 2:
r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t
MATLAB will execute the command above and return the result below:
Column vectors are created by enclosing a set of elements within square brackets, using semicolons (;) to separate elements.
- Example:
c = [7; 8; 9; 10; 11]
MATLAB will execute the command above and return the result below:
Creating Matrices in MATLAB
Matrices in MATLAB are defined as 2-dimensional arrays.
In MATLAB, a matrix is created by entering spaces or commas after each element in a row, and ending the row with a semicolon.
Example of creating a 3-by-3 matrix as shown below:
m = [1 2 3; 4 5 6; 7 8 9]
MATLAB will execute the above command and return the following result:
Hope this Mytour article provides you with useful information on declaring variables in MATLAB. If you have any doubts or questions, feel free to leave your comments below the article, and Mytour will respond to your inquiries as soon as possible.