📖 Scenario: You are learning MATLAB, a tool used by engineers and scientists to solve problems with math and data. Let's start by writing your very first MATLAB program.
🎯 Goal: Create a simple MATLAB script that stores a number, doubles it, and then shows the result.
📋 What You'll Learn
Create a variable called number with the value 10
Create a variable called doubled that is twice the value of number
Display the value of doubled using disp
💡 Why This Matters
🌍 Real World
MATLAB is used in engineering and science to quickly test ideas and solve math problems.
💼 Career
Knowing how to write simple MATLAB programs is useful for jobs in data analysis, engineering, and research.
Progress0 / 4 steps
1
Create the initial variable
Create a variable called number and set it to 10.
MATLAB
Hint
Use the equals sign = to assign the value 10 to the variable number.
2
Create a doubled variable
Create a variable called doubled that is number multiplied by 2.
MATLAB
Hint
Multiply number by 2 using the * operator and assign it to doubled.
3
Display the doubled value
Use disp to display the value of doubled.
MATLAB
Hint
Use disp(doubled); to show the value stored in doubled.
4
Run the program and see the output
Run the program to display the doubled value. The output should be 20.
MATLAB
Hint
Run the script. You should see the number 20 printed in the command window.
Practice
(1/5)
1. What does the MATLAB command disp('Hello, world!') do?
easy
A. It saves the text Hello, world! to a file.
B. It displays the text Hello, world! on the screen.
C. It creates a variable named Hello, world!.
D. It runs a loop that prints Hello, world! multiple times.
Solution
Step 1: Understand the disp function
The disp function in MATLAB shows text or values on the screen.
Step 2: Analyze the input to disp
The input is a string 'Hello, world!' which means it will print this exact text.
Final Answer:
It displays the text Hello, world! on the screen. -> Option B
Quick Check:
disp('text') shows text [OK]
Hint: disp shows text inside single quotes [OK]
Common Mistakes:
Thinking disp saves text to a file
Using double quotes instead of single quotes
Confusing disp with variable creation
2. Which of the following is the correct syntax to display the message Hello in MATLAB?
easy
A. disp('Hello')
B. display(Hello)
C. disp(Hello)
D. print('Hello')
Solution
Step 1: Check the correct function name
MATLAB uses disp to display messages, not display or print.
Step 2: Check the string syntax
Strings must be inside single quotes in MATLAB, so disp('Hello') is correct.
Final Answer:
disp('Hello') -> Option A
Quick Check:
disp with single quotes is correct [OK]
Hint: Use disp with single quotes for text [OK]
Common Mistakes:
Using double quotes instead of single quotes
Using wrong function names like print or display
Missing quotes around text
3. What will be the output of this MATLAB code?
disp('Welcome to MATLAB!')
medium
A. Welcome to MATLAB!
B. disp('Welcome to MATLAB!')
C. SyntaxError
D. No output
Solution
Step 1: Understand disp function behavior
The disp function prints the string inside the parentheses to the screen.
Step 2: Check the string format
The string is correctly enclosed in single quotes, so it will print exactly as given.
Final Answer:
Welcome to MATLAB! -> Option A
Quick Check:
disp prints string text [OK]
Hint: disp prints text inside single quotes exactly [OK]
Common Mistakes:
Expecting disp to print the command itself
Using double quotes causing errors
Forgetting to use disp function
4. Identify the error in this MATLAB code:
disp(Welcome to MATLAB!)
medium
A. Parentheses are not allowed with disp
B. Missing semicolon at the end
C. disp function does not exist
D. Text is not inside single quotes
Solution
Step 1: Check the input to disp
The text Welcome to MATLAB! is not inside single quotes, so MATLAB treats it as variables or commands.
Step 2: Understand string syntax in MATLAB
Strings must be enclosed in single quotes to be recognized as text.
Final Answer:
Text is not inside single quotes -> Option D
Quick Check:
Strings need single quotes [OK]
Hint: Always put text inside single quotes in disp [OK]
Common Mistakes:
Omitting quotes around text
Thinking semicolon is required for disp
Believing disp is not a function
5. You want to write a MATLAB program that displays two lines: Hello and Welcome to MATLAB Which code will do this correctly?
hard
A. disp('Hello\nWelcome to MATLAB')
B. disp('Hello')
disp(Welcome to MATLAB)
C. disp('Hello')
disp('Welcome to MATLAB')
D. disp('Hello' + 'Welcome to MATLAB')
Solution
Step 1: Check how to display multiple lines
Using two separate disp calls prints each line separately.
Step 2: Analyze each option
disp('Hello')
disp('Welcome to MATLAB') uses two disp calls with correct strings. disp('Hello\nWelcome to MATLAB') tries to use \n inside single quotes which MATLAB does not interpret as newline. disp('Hello')
disp(Welcome to MATLAB) misses quotes in second disp. disp('Hello' + 'Welcome to MATLAB') tries to add strings which is invalid in MATLAB.
Final Answer:
disp('Hello')
disp('Welcome to MATLAB') -> Option C
Quick Check:
Use separate disp calls for multiple lines [OK]
Hint: Use separate disp lines for multiple messages [OK]
Common Mistakes:
Using \n inside single quotes expecting newline
Missing quotes around text
Trying to add strings with + operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.