How to import Pickle data using Python Interface

104 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 30 de Mayo de 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

Más respuestas (0)

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by