How to import Pickle data using Python Interface
34 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2023 年 5 月 30 日
回答済み: MathWorks Support Team
2023 年 5 月 30 日
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?
採用された回答
MathWorks Support Team
2023 年 5 月 30 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Call Python from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!