How to import Pickle data using Python Interface
51 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 30 Mai 2023
Réponse apportée : MathWorks Support Team
le 30 Mai 2023
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?
Réponse acceptée
MathWorks Support Team
le 30 Mai 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
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!