Python .pickle file to Matlab .mat file converter

Python .pickle file to Matlab .mat file converter

Not all softwares are capable to do all tasks, we need to switch between multiple programming languages or softwares to do our required tasks.

It becomes a hectic job to convert data from one software to another.

The two softwares that are most popular today for research are python and Matlab.

The artificial intelligence and other research frameworks are usually based on python but doing a comparison, graph generation and many other mathematical tasks, Matlab being the still best.

Python language usually saves variables using pickle files and Matlab saves variable information in a .mat file.

We have created a simple python script, that can convert pickle file to Matlab file:

import numpy, scipy.io
import pickle, sys

if(len(sys.argv) > 2):
	source_name = sys.argv[1]
	dest_name = sys.argv[2]
	
	a=pickle.load( open( source_name, "rb" ) )


	scipy.io.savemat(dest_name, mdict={'pickle_data': a})
	
	print("Data successfully converted to .mat file with variable name \"pickle_data\"")
else:
	print("Usage: pickle_to_mat_converter.py source_name.pickle mat_filename.mat")
	

Python script can be downloaded from here.

 

Usage:

python pickle_to_mat_converter.py source_name.pickle mat_filename.mat

The Matlab file will be generated with having data stored in “pickle_data” variable when the file is loaded in Matlab.

 

Related Links:

Leave a Reply

Your email address will not be published.