0
0
MATLABdata~20 mins

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

Choose your learning style9 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.