🚀 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.whosmat()

AI & DATA SCIENCE // io-whosmat

scipy.io.whosmat() inspects a MATLAB .mat file and lists the names, shapes, and data types of the variables it contains, without actually loading their full data.

Syntax

scipy.io.whosmat(file_name)

Deep Dive Course

whosmat() returns a list of tuples, one per variable in the file, each giving that variable's name, shape, and data type — it's a fast way to check what's inside a .mat file, especially a large one, without paying the cost of actually reading and loading every variable's full array data into memory. This mirrors MATLAB's own built-in 'whos' command, which lists variables currently in the workspace along with similar summary information.

1Understanding io.whosmat()

whosmat() returns a list of tuples, one per variable in the file, each giving that variable's name, shape, and data type — it's a fast way to check what's inside a .mat file, especially a large one, without paying the cost of actually reading and loading every variable's full array data into memory. This mirrors MATLAB's own built-in 'whos' command, which lists variables currently in the workspace along with similar summary information.

💡

Use whosmat() to quickly check a large .mat file's contents, variable names, shapes, sizes, before deciding whether to actually load it, or which specific variables you need — this avoids the potentially significant memory and time cost of loading every variable's full data just to see what's there.

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

savemat("data.mat", {"scores": np.array([85, 90, 78]), "name": "experiment_1"})
info = whosmat("data.mat")
print(info)
localhost:3000

2Practical Example

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

editor.html
from scipy.io import whosmat

info = whosmat("data.mat")
for name, shape, dtype in info:
    print(f"{name}: shape={shape}, dtype={dtype}")
localhost:3000

3Best Practices

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

1. Use whosmat() to inspect a .mat file's contents before loading, especially for large files, rather than loading everything just to check what variables exist

2. Check a variable's reported shape via whosmat() before loading it, to catch unexpected dimensions early

3. Combine whosmat()'s variable name list with a targeted loadmat() call, which can accept a variable_names parameter, to load only the specific variables you actually need

⚠️

Tip: Use whosmat() to quickly check a large .mat file's contents, variable names, shapes, sizes, before deciding whether to actually load it, or which specific variables you need — this avoids the potentially significant memory and time cost of loading every variable's full data just to see what's there.

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

savemat("data.mat", {"scores": np.array([85, 90, 78]), "name": "experiment_1"})
info = whosmat("data.mat")
print(info)
localhost:3000

Examples

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

savemat("data.mat", {"scores": np.array([85, 90, 78]), "name": "experiment_1"})
info = whosmat("data.mat")
print(info)
Example 02Advanced Example
from scipy.io import whosmat

info = whosmat("data.mat")
for name, shape, dtype in info:
    print(f"{name}: shape={shape}, dtype={dtype}")

Best Practices

  • Use whosmat() to inspect a .mat file's contents before loading, especially for large files, rather than loading everything just to check what variables exist
  • Check a variable's reported shape via whosmat() before loading it, to catch unexpected dimensions early
  • Combine whosmat()'s variable name list with a targeted loadmat() call, which can accept a variable_names parameter, to load only the specific variables you actually need

Interview Question

Why would you use whosmat() instead of just calling loadmat() and inspecting the returned dictionary's contents?

Hint: Think about how much actual data each function has to read from disk to give you the information you want.

loadmat() has to read and fully decode every variable's complete array data from the file in order to construct the returned dictionary, which can be slow and memory-intensive for a file containing large arrays, even if you only wanted to check what variables exist and their shapes. whosmat() instead only reads the file's header and structural metadata, the information describing each variable's name, shape, and type, without decoding the actual bulk array data at all, making it a much faster, lower-memory way to answer what's in this file before committing to the cost of actually loading some or all of it.

Exercises

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

savemat("data.mat", {"scores": np.array([85, 90, 78]), "name": "experiment_1"})
info = whosmat("data.mat")
print(info)

Frequently Asked Questions

Why would you use whosmat() instead of just calling loadmat() and inspecting the returned dictionary's contents?

loadmat() has to read and fully decode every variable's complete array data from the file in order to construct the returned dictionary, which can be slow and memory-intensive for a file containing large arrays, even if you only wanted to check what variables exist and their shapes. whosmat() instead only reads the file's header and structural metadata, the information describing each variable's name, shape, and type, without decoding the actual bulk array data at all, making it a much faster, lower-memory way to answer what's in this file before committing to the cost of actually loading some or all of it.

Related Functions

io-loadmatio-savematdf-info