0
0
SciPydata~20 mins

MATLAB file I/O (loadmat, savemat) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MATLAB I/O Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of loading a MATLAB .mat file with nested structs?

Consider a MATLAB .mat file saved with a nested struct variable. When loaded using scipy.io.loadmat, what is the type of the nested struct in Python?

SciPy
import scipy.io
mat = scipy.io.loadmat('nested_struct.mat')
nested = mat['nested_struct']
type_nested = type(nested[0,0]['field1'][0,0])
print(type_nested)
A<class 'scipy.io.matlab.mio5_params.mat_struct'>
B<class 'dict'>
C<class 'numpy.ndarray'>
D<class 'list'>
Attempts:
2 left
💡 Hint

MATLAB structs are loaded as special objects, not plain dicts.

data_output
intermediate
1:30remaining
How many variables are loaded from a .mat file with 3 variables?

You load a MATLAB file containing exactly 3 variables using scipy.io.loadmat. How many keys will the resulting dictionary have, excluding MATLAB metadata keys?

SciPy
import scipy.io
mat = scipy.io.loadmat('three_vars.mat')
keys = [k for k in mat.keys() if not k.startswith('__')]
print(len(keys))
A1
B5
C0
D3
Attempts:
2 left
💡 Hint

MATLAB metadata keys start and end with double underscores.

🔧 Debug
advanced
2:30remaining
Why does saving a dictionary with a list value cause an error in savemat?

Given the code below, why does scipy.io.savemat raise an error?

import scipy.io
data = {'arr': [1, 2, 3]}
scipy.io.savemat('file.mat', data)
SciPy
import scipy.io
data = {'arr': [1, 2, 3]}
scipy.io.savemat('file.mat', data)
Asavemat requires all values to be strings
Bsavemat expects numpy arrays, not Python lists
Csavemat cannot save dictionaries
Dsavemat only saves scalar values
Attempts:
2 left
💡 Hint

Check the data type of values passed to savemat.

🚀 Application
advanced
2:30remaining
How to save a Python dictionary with mixed data types to a .mat file?

You want to save a Python dictionary with keys 'name' (string), 'age' (int), and 'scores' (list of floats) to a MATLAB .mat file. Which approach works correctly?

AConvert 'scores' to numpy array, then use savemat
BSave the dictionary directly without changes
CConvert 'age' to string, then save
DConvert all values to lists, then save
Attempts:
2 left
💡 Hint

MATLAB expects arrays or scalars, not Python lists or mixed types.

🧠 Conceptual
expert
3:00remaining
What happens to MATLAB cell arrays when loaded with loadmat?

When loading a MATLAB .mat file containing a cell array using scipy.io.loadmat, how is the cell array represented in Python?

AAs a dict with keys as indices
BAs a Python list of lists
CAs a numpy ndarray with dtype=object containing elements
DAs a pandas DataFrame
Attempts:
2 left
💡 Hint

Cell arrays hold mixed types and are loaded as arrays with object dtype.