Variables let you store information to use later in your program. Assignment means giving a value to a variable.
Variable creation and assignment in MATLAB
variableName = value;
x and assigns it the number 5.x = 5;name and assigns it the text 'Alice'.name = 'Alice';piValue.piValue = 3.1416;result the sum of x and 10.result = x + 10;This program creates two variables a and b, adds them, and shows the result.
a = 10; b = 20; sumVal = a + b; disp(['Sum of a and b is: ' num2str(sumVal)])
Variable names are case-sensitive: a and A are different.
Use disp to show values on the screen.
Ending a line with a semicolon ; hides the output in the command window.
Variables store data with a name you choose.
Use = to assign values to variables.
Semicolons keep your workspace clean by hiding output.
Practice
What does the following MATLAB code do?x = 10;
Solution
Step 1: Understand variable assignment syntax
The code uses=to assign a value to a variable.Step 2: Identify the variable and value
Variablexis assigned the value10.Final Answer:
Creates a variable named x and assigns it the value 10 -> Option AQuick Check:
Variable assignment = Creates variable with value [OK]
- Thinking '=' prints output
- Confusing variable creation with function definition
- Missing semicolon means output prints but variable still created
Which of the following is the correct way to assign the value 5 to variable a in MATLAB?
Solution
Step 1: Recall MATLAB assignment operator
MATLAB uses=to assign values to variables.Step 2: Check each option
a = 5; usesa = 5;which is correct syntax.Final Answer:
a = 5; -> Option BQuick Check:
Assignment uses '=' in MATLAB [OK]
- Using '==' which is a comparison, not assignment
- Trying to assign value to a number (5 = a)
- Using ':=' which is not MATLAB syntax
What is the value of variable y after running this code?
y = 3; y = y + 4;
Solution
Step 1: Assign initial value to y
Variableyis first set to 3.Step 2: Add 4 to current value of y
New value is 3 + 4 = 7.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Thinking y stays 3 after addition
- Confusing assignment with comparison
- Expecting syntax error due to reuse of variable
Identify the error in this MATLAB code:
1var = 10;
Solution
Step 1: Check variable naming rules
MATLAB variable names must start with a letter, not a number.Step 2: Analyze given variable name
Variable name1varstarts with '1', which is invalid.Final Answer:
Variable names cannot start with a number -> Option DQuick Check:
Variable names start with letters [OK]
- Thinking semicolon is mandatory for syntax
- Confusing assignment operator '=' with '=='
- Assuming numbers can start variable names
You want to create two variables a and b where a is 5 and b is twice a. Which code correctly does this?
a = 5; b = 2 * a;
Solution
Step 1: Assign value 5 to variable a
Codea = 5;correctly assigns 5 toa.Step 2: Assign b as twice a
Codeb = 2 * a;correctly calculates twice the value ofa.Final Answer:
a = 5; b = 2 * a; -> Option CQuick Check:
b = 2 * a means b is double a [OK]
- Adding instead of multiplying for double
- Dividing instead of multiplying
- Multiplying variable by itself instead of 2
Ready to prove it?
Pass the test and this pattern moves unknown
