Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

SciPy Matlab Arrays


Working With Matlab Arrays

We know that NumPy provides us with methods to persist the data in readable formats for Python. But SciPy provides us with interoperability with Matlab as well.

SciPy provides us with the module scipy.io, which has functions for working with Matlab arrays.


Exporting Data in Matlab Format

The savemat() function allows us to export data in Matlab format.

The method takes the following parameters:

  1. filename - the file name for saving data.
  2. mdict - a dictionary containing the data.
  3. do_compression - a boolean value that specifies whether to compress the result or not. Default False.

Example

Export the following array as variable name "vec" to a mat file:

from scipy import io
import numpy as np

arr = np.arange(10)

io.savemat('arr.mat', {"vec": arr})

Note: The example above saves a file name "arr.mat" on your computer.

To open the file, check out the "Import Data from Matlab Format" example below:



Import Data from Matlab Format

The loadmat() function allows us to import data from a Matlab file.

The function takes one required parameter:

filename - the file name of the saved data.

It will return a structured array whose keys are the variable names, and the corresponding values are the variable values.

Example

Import the array from following mat file.:

from scipy import io
import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,])

# Export:
io.savemat('arr.mat', {"vec": arr})

# Import:
mydata = io.loadmat('arr.mat')

print(mydata)

Result:


 {
   '__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Tue Sep 22 13:12:32 2020',
   '__version__': '1.0',
   '__globals__': [],
   'vec': array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
 }

Try it Yourself »

Use the variable name "vec" to display only the array from the matlab data:

Example

...

print(mydata['vec'])

Result:


 [[0 1 2 3 4 5 6 7 8 9]]

Try it Yourself »

Note: We can see that the array originally was 1D, but on extraction it has increased one dimension.

In order to resolve this we can pass an additional argument squeeze_me=True:

Example

# Import:
mydata = io.loadmat('arr.mat', squeeze_me=True)

print(mydata['vec'])

Result:


 [0 1 2 3 4 5 6 7 8 9]

Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.