🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

io.loadmat()

AI & DATA SCIENCE // io-loadmat

scipy.io.loadmat() loads variables from a MATLAB .mat file into Python, returning them as a dictionary of NumPy arrays.

Syntax

scipy.io.loadmat(file_name)

Deep Dive Course

loadmat() returns a dict where each MATLAB variable's name becomes a key and its data becomes a NumPy array value, along with a handful of extra metadata keys, like header, version, and globals keys, that describe the file itself rather than actual saved variables. As with savemat(), any 1D array data ends up represented as a 2D array, a single row or column, since that's how MATLAB itself represents it, which is important to remember when using the loaded data in further NumPy computations expecting a flat 1D shape.

1Understanding io.loadmat()

loadmat() returns a dict where each MATLAB variable's name becomes a key and its data becomes a NumPy array value, along with a handful of extra metadata keys, like header, version, and globals keys, that describe the file itself rather than actual saved variables. As with savemat(), any 1D array data ends up represented as a 2D array, a single row or column, since that's how MATLAB itself represents it, which is important to remember when using the loaded data in further NumPy computations expecting a flat 1D shape.

💡

Filter out the metadata keys, those starting and ending with double underscores, when iterating over a loaded .mat file's variables — loadmat() always includes them alongside your actual saved data.

editor.html
from scipy.io import savemat, loadmat
import numpy as np

savemat("data.mat", {"scores": np.array([85, 90, 78])})
loaded = loadmat("data.mat")
print(loaded["scores"])
localhost:3000

2Practical Example

Here is a real-world application of io.loadmat() showing how it is used in production SciPy code.

editor.html
from scipy.io import savemat, loadmat
import numpy as np

savemat("data.mat", {"scores": np.array([85, 90, 78])})
loaded = loadmat("data.mat")
print(list(loaded.keys()))
localhost:3000

3Best Practices

Follow these guidelines when working with io.loadmat():

1. Filter out or skip the dunder metadata keys when iterating over loadmat()'s returned dict, since they aren't actual saved variables

2. Use .squeeze() or .ravel() on loaded array data if you specifically need it back in a flat 1D shape instead of MATLAB's inherent 2D row/column representation

3. Use io.whosmat() first to inspect a .mat file's variable names and shapes before loading its full contents, if you only need to check what's inside without loading all the actual data

⚠️

Tip: Filter out the metadata keys, those starting and ending with double underscores, when iterating over a loaded .mat file's variables — loadmat() always includes them alongside your actual saved data.

editor.html
from scipy.io import savemat, loadmat
import numpy as np

savemat("data.mat", {"scores": np.array([85, 90, 78])})
loaded = loadmat("data.mat")
print(loaded["scores"])
localhost:3000

Examples

Example 01Basic Usage
from scipy.io import savemat, loadmat
import numpy as np

savemat("data.mat", {"scores": np.array([85, 90, 78])})
loaded = loadmat("data.mat")
print(loaded["scores"])
Example 02Advanced Example
from scipy.io import savemat, loadmat
import numpy as np

savemat("data.mat", {"scores": np.array([85, 90, 78])})
loaded = loadmat("data.mat")
print(list(loaded.keys()))

Best Practices

  • Filter out or skip the dunder metadata keys when iterating over loadmat()'s returned dict, since they aren't actual saved variables
  • Use .squeeze() or .ravel() on loaded array data if you specifically need it back in a flat 1D shape instead of MATLAB's inherent 2D row/column representation
  • Use io.whosmat() first to inspect a .mat file's variable names and shapes before loading its full contents, if you only need to check what's inside without loading all the actual data

Interview Question

Why does loadmat()'s returned dictionary include keys like '__header__' and '__version__' alongside your actual saved variables?

Hint: Think about what those specific keys describe, compared to the data you actually saved.

Those double-underscore keys describe metadata about the .mat file itself, like which MATLAB version's format it was written in and header information from the file, rather than being variables you actually saved into it — loadmat() includes them because they're technically part of what's stored in the file's structure, alongside your actual data. Since they're not variables you created, code that iterates over the loaded dictionary's contents typically needs to explicitly skip or filter out these metadata keys to correctly process just the actual saved data.

Exercises

MediumPractice using io.loadmat() in a real scenario.
View Solution
from scipy.io import savemat, loadmat
import numpy as np

savemat("data.mat", {"scores": np.array([85, 90, 78])})
loaded = loadmat("data.mat")
print(loaded["scores"])

Frequently Asked Questions

Why does loadmat()'s returned dictionary include keys like '__header__' and '__version__' alongside your actual saved variables?

Those double-underscore keys describe metadata about the .mat file itself, like which MATLAB version's format it was written in and header information from the file, rather than being variables you actually saved into it — loadmat() includes them because they're technically part of what's stored in the file's structure, alongside your actual data. Since they're not variables you created, code that iterates over the loaded dictionary's contents typically needs to explicitly skip or filter out these metadata keys to correctly process just the actual saved data.

Related Functions

io-savematio-whosmatnp-load