0
0
MATLABdata~20 mins

MAT file save and load in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MAT File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
AError: Undefined variable 'a'.
B0
C15
D5
Attempts:
2 left
💡 Hint
Remember that save stores variables to a file and load restores them into the workspace.
Predict Output
intermediate
2: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;
AError: Variable 'y' not found in file.mat
Bvars is empty (no variables loaded)
Cvars contains variable 'y' with value 42
Dvars contains variable 'x' with value 42
Attempts:
2 left
💡 Hint
Loading a variable name that does not exist in the MAT file does not load anything.
🔧 Debug
advanced
2: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');
ANo error, variable 'Data' is loaded correctly
BError because file 'mydata.mat' does not exist
CError because 'clear data' removes the file
DError because variable name 'Data' does not match saved variable 'data' (case sensitive)
Attempts:
2 left
💡 Hint
MATLAB variable names are case sensitive when loading specific variables.
📝 Syntax
advanced
2: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'.
Asave('vars.mat', 'a', 'b', 'c');
Bsave('vars.mat', a, b, c);
Csave('vars.mat', [a b c]);
Dsave('vars.mat', {'a', 'b', 'c'});
Attempts:
2 left
💡 Hint
Variable names must be passed as strings to the save function.
🚀 Application
expert
2: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;
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Only variables specified in the load command are loaded.