Bird
Raised Fist0
MATLABdata~20 mins

Script files and editor in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Script Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple script file
What is the output when you run this MATLAB script file named example.m?
MATLAB
a = 5;
b = 3;
c = a + b;
disp(c);
A8
B53
Ca + b
DError: Undefined function or variable 'c'.
Attempts:
2 left
💡 Hint
Remember that disp shows the value of the variable, not the expression itself.
❓ Predict Output
intermediate
2:00remaining
Variable scope in script files
Given this script myscript.m:
a = 10;
b = 20;
c = a + b;
What will be the value of c in the MATLAB command window after running myscript?
MATLAB
a = 10;
b = 20;
c = a + b;
A0
B30
CError: Variable 'c' is undefined.
D30 but only inside the script, not in the command window.
Attempts:
2 left
💡 Hint
Script files share the workspace with the command window.
🔧 Debug
advanced
2:00remaining
Identify the error in this script
What error will MATLAB show when running this script file named test.m?
MATLAB
x = 5
if x > 3
    disp('x is greater than 3');
end
ANo error, output: x is greater than 3
BSyntax error: Missing semicolon after first line
CSyntax error: Missing 'end' for if statement
DRuntime error: Undefined variable 'x'
Attempts:
2 left
💡 Hint
In MATLAB, semicolons are optional to end statements.
❓ Predict Output
advanced
2:00remaining
Effect of clearing variables in script
Consider this script clear_test.m:
clear
x = 7;
y = x * 2;
disp(y);
What will be the output when you run clear_test?
MATLAB
clear
x = 7;
y = x * 2;
disp(y);
A0
BError: Undefined variable 'x'.
C14
DNo output, script clears all variables and stops.
Attempts:
2 left
💡 Hint
The clear command removes variables before new ones are created.
🧠 Conceptual
expert
3:00remaining
Script file behavior with function definitions
What happens if you define a function inside a MATLAB script file and then call it within the same script?
MATLAB
disp(myfunc(3));

function y = myfunc(x)
    y = x^2;
end
AError: Functions cannot be defined inside script files
BThe script runs but outputs nothing
CError: Function 'myfunc' is undefined when called
DThe script runs and outputs 9
Attempts:
2 left
💡 Hint
Since MATLAB R2016b, local functions can be included in scripts.

Practice

(1/5)
1. What is the main purpose of a script file in MATLAB?
easy
A. To create graphical user interfaces
B. To save and run a group of commands together
C. To store variables permanently
D. To compile MATLAB code into an executable

Solution

  1. Step 1: Understand what script files do

    Script files contain a sequence of MATLAB commands saved in a file to run together.
  2. Step 2: Compare options with script file purpose

    Only To save and run a group of commands together describes saving and running commands together, which is the main use of scripts.
  3. Final Answer:

    To save and run a group of commands together -> Option B
  4. Quick Check:

    Script files = save and run commands [OK]
Hint: Scripts group commands to run all at once [OK]
Common Mistakes:
  • Confusing scripts with functions
  • Thinking scripts store variables permanently
  • Assuming scripts compile code
2. Which of the following is the correct way to save a script file in MATLAB Editor?
easy
A. Click File > Save or press Ctrl+S
B. Type save script in the command window
C. Right-click the command window and select Save
D. Use the command saveas('filename') in the script

Solution

  1. Step 1: Identify how to save files in MATLAB Editor

    The Editor uses standard save options like File menu or Ctrl+S shortcut.
  2. Step 2: Evaluate each option

    Only Click File > Save or press Ctrl+S correctly describes saving via Editor menu or shortcut. Others are incorrect or commands for different purposes.
  3. Final Answer:

    Click File > Save or press Ctrl+S -> Option A
  4. Quick Check:

    Save script = File menu or Ctrl+S [OK]
Hint: Use File menu or Ctrl+S to save scripts [OK]
Common Mistakes:
  • Trying to save scripts from command window
  • Using saveas command incorrectly
  • Right-clicking command window instead of Editor
3. What will be the output when running this script in MATLAB Editor?
x = 5;
y = 3;
z = x + y;
disp(z);
medium
A. 8
B. 53
C. x + y
D. Error: Undefined variable

Solution

  1. Step 1: Understand the script commands

    Variables x and y are assigned 5 and 3, then z is sum of x and y.
  2. Step 2: Calculate the value of z and output

    z = 5 + 3 = 8, disp(z) prints 8 to the command window.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    5 + 3 = 8 output [OK]
Hint: Add variables then display result [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Expecting variable names printed
  • Assuming error without variable initialization
4. Identify the error in this MATLAB script:
for i = 1:3
  disp(i)
endd
medium
A. disp function used incorrectly
B. Missing semicolon after disp(i)
C. Loop variable i is not defined
D. Incorrect end keyword spelling

Solution

  1. Step 1: Check the loop syntax

    The for loop uses 'endd' instead of 'end' to close the loop.
  2. Step 2: Identify the syntax error

    MATLAB requires 'end' keyword exactly; 'endd' causes a syntax error.
  3. Final Answer:

    Incorrect end keyword spelling -> Option D
  4. Quick Check:

    Loop ends with 'end' keyword [OK]
Hint: Use exact 'end' to close loops [OK]
Common Mistakes:
  • Typing 'endd' instead of 'end'
  • Forgetting to close loops
  • Adding unnecessary semicolons
5. You want to create a script that calculates the average of numbers stored in a vector data. Which script structure correctly computes and displays the average?
hard
A.
avg = mean(data)
disp(total);
B.
for i = 1:length(data)
  avg = data(i) / length(data);
end
disp(avg);
C.
total = sum(data);
count = length(data);
avg = total / count;
disp(avg);
D.
avg = sum(data) * length(data);
disp(avg);

Solution

  1. Step 1: Understand how to calculate average

    Average = sum of elements divided by number of elements.
  2. Step 2: Check each script option

    total = sum(data);
    count = length(data);
    avg = total / count;
    disp(avg);
    correctly sums data, counts elements, divides, then displays average. Others have errors: A displays undefined total, C overwrites avg incorrectly in loop, D multiplies instead of dividing.
  3. Final Answer:

    total = sum(data); count = length(data); avg = total / count; disp(avg); -> Option C
  4. Quick Check:

    Average = sum / count [OK]
Hint: Average = sum divided by count, then display [OK]
Common Mistakes:
  • Using mean without displaying correct variable
  • Calculating average inside loop incorrectly
  • Multiplying sum by length instead of dividing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard