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?
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)
MATLAB structs are loaded as special objects, not plain dicts.
When loading MATLAB structs with loadmat, nested structs become mat_struct objects, which behave like objects with attributes.
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?
import scipy.io mat = scipy.io.loadmat('three_vars.mat') keys = [k for k in mat.keys() if not k.startswith('__')] print(len(keys))
MATLAB metadata keys start and end with double underscores.
The loaded dictionary contains all variables as keys plus metadata keys starting with '__'. Filtering those out leaves exactly the number of variables saved.
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)import scipy.io data = {'arr': [1, 2, 3]} scipy.io.savemat('file.mat', data)
Check the data type of values passed to savemat.
savemat requires values to be numpy arrays or scalars. Python lists are not accepted and cause a TypeError.
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?
MATLAB expects arrays or scalars, not Python lists or mixed types.
To save mixed data types, convert lists to numpy arrays and keep strings and ints as is. Then savemat can save the dictionary properly.
When loading a MATLAB .mat file containing a cell array using scipy.io.loadmat, how is the cell array represented in Python?
Cell arrays hold mixed types and are loaded as arrays with object dtype.
MATLAB cell arrays become numpy ndarrays with dtype=object, where each element corresponds to a cell content.