SciPy - savemat() Function



The scipy.io.savemat() method converts Python data structures, such as NumPy arrays or dictionaries, into the MATLAB.mat file format. This allows for smooth interoperability between Python and MATLAB, thereby sharing data across different environments.

This method is common in scientific computing, machine learning, and data analysis. where, Python is used for processing and MATLAB is preferred for modelling and visualization. It is particularly useful in exporting large datasets or further analysis in MATLAB.

This approach offers many options, including changing file format versions, enabling compression, and managing how 1D arrays are saved. The approach is compatible with MATLAB versions up to 7.3 and utilizes HDF5 for bigger datasets to increase efficiency.

In real-world applications, savemat() is often used in combination with loadmat() to transfer data between Python and MATLAB. It can be integrated into workflows where Python handles data manipulation and MATLAB performs specialized calculations or visualizations.

Syntax

The syntax for the Scipy savemat() method is as follows −

.savemat(file_name, mdict, appendmat=True, format='5', long_field_names=False, do_compression=False, oned_as='row')

Parameters

This method accepts the following parameters −

  • file_name (str) − The name of the .mat file to save. If the file already exists, it will be overwritten unless appendmat=False is specified. Ifappendmat==True .mat extension not needed

  • mdict (dict) − A dictionary containing the data to save. Keys are variable names, and values are the corresponding data (arrays or other types).

  • appendmat (bool) − If True, the.mat extension is added automatically to the file_name if it is not already there. Set False if you do not want the extension added.

  • format (str, optional, default='5') − Specifies the version of MATLAB that will be used to save the file. '5' indicates the v5 format, which is MATLAB 5.0 compatible and later. '4' saves the file in the MATLAB v4 format that does not support some of the features, such as nested structures.

  • long_field_names (bool, optional, default=False) − If True, long field names (longer than 31 characters) are allowed in MATLAB structures. If False, long field names will be truncated to 31 characters.

  • do_compression (bool, optional) − If True, saves the file in compressed mat format. This reduces file size, but it may slow down saving and loading if True, saves the file in compressed mat format. This reduces file size, but it may slow down saving and loading.

  • oned_as (str, optional, default='row') − Controls how one-dimensional arrays are preserved. If 'row' is specified, 1D arrays are saved as 1xN arrays. If 'column' is selected, the data is saved as Nx1 arrays.

Return Value

The savemat() function does not return anything. It just saves the information provided to the .mat file specified. If everything goes well, it returns nothing; otherwise, it reports an error if anything goes wrong.

Example 1: Saving Data using savemat() method

This is the basic example of savemat(), where we have created a simple dictionary containing a 2D list and saved it to a .mat file. This is useful for sharing or storing data in a MATLAB-readable format.

import scipy
from scipy.io import savemat

# Create a simple Python dictionary with data
data_to_save = {'my_matrix': [[1, 2, 3], [4, 5, 6]]}

# Save the dictionary to a .mat file
scipy.io.savemat('example.mat', data_to_save)
print("data successfully saved to example.mat")

When we run above program, it produces following result

print("data successfully saved to example.mat")

Example 2: Column Vector Example

The oned_as parameter in savemat() specifies how 1D arrays are stored, making it possible to save them as column vectors for MATLAB compatibility.

In the below code, we have created a 1D NumPy array and saved as a column vector using oned_as='column'. This ensures that MATLAB correctly interprets the array's shape during data exchange.

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

# Save the 1D array as a column vector
data_to_save = {'my_vector': np.array([1, 2, 3])}
savemat('column_vector.mat', data_to_save, oned_as='column')

# Load and inspect the saved data
loaded_data = loadmat('column_vector.mat')
print(loaded_data)

Following is an output of the above code

{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Wed Dec 18 09:17:46 2024', '__version__': '1.0', '__globals__': [], 'my_vector': array([[1],
       [2],
       [3]])}

Example 3: Compressing data with do_compression in savemat()

The do_compression parameter in savemat() compresses the output file, reducing its size while retaining all data, which is useful for large datasets.

In this example let us create a 2D NumPy array and save the data to example.mat file and also use do_compression = True to save the file in compress mat format.

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

# Save the NumPy array with compression enabled
data_to_save = {'my_array': np.array([[1, 2], [3, 4]])}
savemat('compressed_example.mat', data_to_save, do_compression=True)
print("Compression done.")

# Load and inspect the saved data
loaded_data = loadmat('compressed_example.mat')
print("\nLoaded data from 'compressed_example.mat':")
print(loaded_data)

Output of the above code is as follows

Compression done.

Loaded data from 'compressed_example.mat':
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Wed Dec 18 09:09:29 2024', '__version__': '1.0', '__globals__': [], 'my_array': array([[1, 2],
       [3, 4]])}

Example 4: Using long_field_names to Save Extended Field Names

The long_field_names parameter in savemat() enables support for field names longer than 31 characters.

In the below example we have created a structure with long field names and saved the data to long_field_names.mat and used long_fields_names parameter = True this allows to store the field name longer than 31 characters to the MATLAB struture.

import scipy
from scipy.io import savemat, loadmat

# Save the structure with long field names allowed
data_to_save = {'my_struct': {'field1': [1, 2], 'field_with_very_long_name': [3, 4]}}
scipy.io.savemat('long_field_names.mat', data_to_save, long_field_names=True)

# Load and inspect the saved data
loaded_data = loadmat('long_field_names.mat')
print("\nLoaded data from 'long_field_names.mat':")
print(loaded_data)

Output of the above code is as follows

Loaded data from 'long_field_names.mat':
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Wed Dec 18 09:13:59 2024', '__version__': '1.0', '__globals__': [], 'my_struct': array([[(array([[1, 2]]), array([[3, 4]]))]],
      dtype=[('field1', 'O'), ('field_with_very_long_name', 'O')])}
scipy_input_output.htm
Advertisements