0
0
SciPydata~10 mins

MATLAB file I/O (loadmat, savemat) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MATLAB file I/O (loadmat, savemat)
Start: Prepare data in Python
Use savemat() to save data to .mat file
File saved on disk
Use loadmat() to read .mat file
Data loaded back into Python
Use data for analysis or display
End
This flow shows saving Python data to a MATLAB .mat file and then loading it back into Python using scipy functions.
Execution Sample
SciPy
from scipy.io import savemat, loadmat

# Save data
data = {'x': [1, 2, 3], 'y': [4, 5, 6]}
savemat('datafile.mat', data)

# Load data
loaded = loadmat('datafile.mat')
print(loaded)
This code saves a dictionary to a .mat file and then loads it back, printing the loaded data.
Execution Table
StepActionInput/VariableResult/Output
1Prepare data dictionarydata = {'x': [1,2,3], 'y': [4,5,6]}{'x': [1,2,3], 'y': [4,5,6]}
2Call savemat()savemat('datafile.mat', data)File 'datafile.mat' created with variables x and y
3Call loadmat()loadmat('datafile.mat')Dictionary with keys: '__header__', '__version__', '__globals__', 'x', 'y'
4Print loaded dataprint(loaded){'__header__': ..., '__version__': ..., '__globals__': ..., 'x': array([[1], [2], [3]]), 'y': array([[4], [5], [6]])}
5EndN/AData loaded successfully from .mat file
💡 Data saved and loaded successfully; loadmat adds metadata keys automatically
Variable Tracker
VariableStartAfter savematAfter loadmatFinal
data{'x': [1,2,3], 'y': [4,5,6]}No changeNo changeNo change
loadedN/AN/A{'__header__': ..., 'x': array([[1], [2], [3]]), 'y': array([[4], [5], [6]])}Same as after loadmat
Key Moments - 2 Insights
Why does loadmat output include keys like '__header__' and '__version__'?
loadmat always adds metadata keys to the loaded dictionary to store file info. Your saved variables appear as keys alongside these.
Why are the loaded arrays wrapped in 2D arrays like array([[1], [2], [3]])?
MATLAB stores arrays as 2D by default, so loadmat returns them as 2D numpy arrays even if originally 1D lists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does savemat() do at step 2?
ALoads data from a .mat file
BPrints the data dictionary
CCreates a .mat file with the given variables
DDeletes the .mat file
💡 Hint
See step 2 in execution_table where savemat creates the file
At which step does loadmat() add metadata keys like '__header__'?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check step 3 in execution_table where loadmat returns dictionary with metadata keys
If the original data had a variable 'z', what would happen in the loaded dictionary?
AThe loaded dictionary would exclude 'z'
BThe loaded dictionary would include key 'z' with its data
CThe file would fail to save
DThe loaded dictionary would only have metadata keys
💡 Hint
savemat saves all keys; loadmat loads all saved variables as keys
Concept Snapshot
Use savemat(filename, dict) to save Python dict data to a MATLAB .mat file.
Use loadmat(filename) to load .mat file data back into Python as a dict.
loadmat adds metadata keys like '__header__' automatically.
Arrays load as numpy arrays, often 2D by default.
This allows easy data exchange between Python and MATLAB.
Full Transcript
This visual execution trace shows how to save Python data to a MATLAB .mat file using scipy's savemat function, and then load it back using loadmat. First, a Python dictionary with lists is prepared. Then savemat writes this data to a file named 'datafile.mat'. Next, loadmat reads this file and returns a dictionary that includes the saved variables plus some metadata keys like '__header__'. The loaded arrays appear as 2D numpy arrays. This process allows sharing data between Python and MATLAB easily.