python-scipy-matlab-arrays

Introduction to Python SciPy Matlab Arrays

In This Article, You Will Learn About Python SciPy Matlab arrays.

Python SciPy Matlab arrays – Before moving ahead, let’s know a bit about Python SciPy Spatial Data

Table of Contents

Getting start with Matlab Arrays

As we know, that NumPy provide us methods to keep data in readable format in Python. But in other hand, SciPy provide us with interoperability with Matlab as well.

So, to work with Matlab arrays, SciPy provides module scipy.io.

Export Data in Matlab Format

To export data in Matlab format, we have got a function savemat().

It includes the following parameters –

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

Example – Creating a file named my_file to export data in Matlab format.

				
					from scipy import io
import numpy as np

array = np.arange(8)

io.savemat("array.matfile", {"my_dictionary" : Array})
    
				
			

Note: Above code will create a file in your IDE or system named “array.matfile”.

Note: To open the file, consider the example given below.

Import Data from Matlab Format

To import data from a Matlab file, we use function loadmat().

It includes only one parameter.

filename – The file name of the saved data.

It will provide a structured array, with keys that are the variable names and the opposite values are the variables values.

Example – Importing the array from mat file.

				
					from scipy import io
import numpy as np

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

io.savemat("array.matfile", {"my_dictionary" : array})
data =io.loadmat("array.matfile")

print(data)

				
			
python-scipy-matlab-arrays

It is clear that it returned data stored in the dictionary, but it is not only data returned. It also produced the date when the file ran.


So, to get only dictionary data, consider giving an example below.

Now, to display only the array from the matlab data, using the variable name “my_dictionary“.

				
					from scipy import io
import numpy as np

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

# Exporting:
io.savemat('array.matfile', {"my_dictionary": array})

# Importing:
mydata = io.loadmat('array.matfile')

print(mydata['my_dictionary'])

				
			
				
					Output - 

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

As it is clear, it returned only dictionary data but not in the original dimension.

Note: We know that array was originally 1-D, but it has increased its dimensions after extraction.

To solve this problem, now pass an extra argument called squeeze_me=True.

Example – Getting the array in original dimension.

				
					from scipy import io
import numpy as np

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

# Exporting:
io.savemat('array.matfile', {"my_dictionary": array})

# Importing:
mydata = io.loadmat('array.matfile', squeeze_me=True)

print(mydata['my_dictionary'])

				
			
				
					Output - 

[0 1 2 3 4 5 6 7 8]
				
			

As it is clear, it returned dictionary data in its original dimension, i.e., 1-D.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on