How to import Pickle data using Python Interface

111 views (last 30 days)
I created data in Python and serialized it in a Pickle file using the following code.
import pickle
# Create a Python dictionary whose values are tuples
data = {'x': (1, 2, 3), 'y': (3.1, 4.2, 5.3)}
with open('data.pickle','wb') as f:
pickle.dump(data,f,pickle.HIGHEST_PROTOCOL)
How can I load and use this data in MATLAB?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 May 2023
You can load Pickle-formatted data into MATLAB by using Python Interface . To call Python modules from MATLAB you must first configure your system to use Python . Note that Pickle is preinstalled with Python 3.x and does not need to be installed.
To load your Pickle data into MATLAB, execute these commands.
>> fid = py.open('data.pickle','rb');
>> data = py.pickle.load(fid)
data =
  Python dict with no properties.
    {'x': (1, 2, 3), 'y': (3.1, 4.2, 5.3)}
For more information on how to work with the imported data, see Handle Data Returned from Python Function and Access Elements in Python Container Types . For example, the following code creates a MATLAB double array from the dictionary 'y' value.
>> y = double(data{"y"})
y =
3.1000 4.2000 5.3000

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!