Challenge - 5 Problems
MAT File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after saving and loading variables?
Consider the following MATLAB code that saves variables to a MAT file and then loads them back. What will be the value of
result after running all commands?MATLAB
a = 5; b = 10; save('data.mat', 'a', 'b'); clear a b load('data.mat'); result = a + b;
Attempts:
2 left
💡 Hint
Remember that
save stores variables to a file and load restores them into the workspace.✗ Incorrect
The variables
a and b are saved to 'data.mat'. After clearing them, load brings them back. So, result = a + b = 5 + 10 = 15.❓ Predict Output
intermediate2:00remaining
What happens when loading a MAT file with a missing variable?
Given the following MATLAB commands, what will be the output of
whos after loading?MATLAB
x = 42; save('file.mat', 'x'); clear x load('file.mat', 'y'); vars = whos;
Attempts:
2 left
💡 Hint
Loading a variable name that does not exist in the MAT file does not load anything.
✗ Incorrect
The MAT file contains only variable 'x'. Loading 'y' which does not exist results in no variables loaded. So
vars is empty.🔧 Debug
advanced2:00remaining
Why does this MAT file load fail with an error?
Examine the code below. It tries to save and load a variable but causes an error on load. What is the cause?
MATLAB
data = rand(3); save('mydata.mat', 'data'); clear data load('mydata.mat', 'Data');
Attempts:
2 left
💡 Hint
MATLAB variable names are case sensitive when loading specific variables.
✗ Incorrect
The saved variable is named 'data' (lowercase). Loading 'Data' (uppercase D) fails because it does not exist in the file.
📝 Syntax
advanced2:00remaining
Which option correctly saves multiple variables to a MAT file?
Select the correct MATLAB command to save variables
a, b, and c into a file named 'vars.mat'.Attempts:
2 left
💡 Hint
Variable names must be passed as strings to the save function.
✗ Incorrect
The save function requires variable names as strings to save them. Option A correctly passes the names as strings.
🚀 Application
expert2:00remaining
How many variables are loaded from this MAT file?
Suppose a MAT file 'mixed.mat' contains variables
x, y, and z. What is the number of variables in the workspace after running this code?MATLAB
load('mixed.mat', 'x', 'z'); vars = whos;
Attempts:
2 left
💡 Hint
Only variables specified in the load command are loaded.
✗ Incorrect
Only variables 'x' and 'z' are loaded, so
vars contains 2 variables.