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.
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)2Practical Example
Here is a real-world application of io.whosmat() showing how it is used in production SciPy code.
from scipy.io import whosmat
info = whosmat("data.mat")
for name, shape, dtype in info:
print(f"{name}: shape={shape}, dtype={dtype}")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.
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)