How can i open dataset in matlab which is .pkl format?
Afficher commentaires plus anciens
I get a dataset in pickle format From a Author,but i want to do my project in matlab. so which command in matlab which help to open and use that datast.
1 commentaire
kc
le 31 Déc 2019
Is there any way, i am also gt stuck here.
plz help
Réponses (2)
Tim R
le 20 Jan 2020
Here's a quick solution based on the links assuming you've set-up Matlab to point to the right Python ver and libraries:
fid=py.open(filename,'rb');
data=py.pickle.load(fid);
The contents of data will need to be converted from their respective python classes to those used by Matlab. For example, if data is a list then convert to a cell:
data=cell(data);
Similarly, dictionaries can be converted to strucures
data=struct(data)
And numpy arrays to doubles
data=double(data);
8 commentaires
kc
le 21 Jan 2020
Thank you so much for your reply. Unfortunately i dnt know anything about python.
Now i have a text file in which path of that fiile is given where my data is saved (actually this is not my system path i need to save it on that location, somehow i will manage that) . So now in MATLAB i need to extract that file location and eventually read that data. everytime i need to store in some folders.
I have created cell from that text file, so can i read that cell data as a path now ?
Walter Roberson
le 21 Jan 2020
filename = fileread('NameOfFileThatHasPathToDataFile');
fid=py.open(filename,'rb');
data=py.pickle.load(fid);
kc
le 21 Jan 2020
Sir thanks to you too,but this is not working in matlab. and now no need of that pickle file. Only that path file is required,
Thanks :)
Quan minhquanccna
le 22 Mar 2021
Thank you for helping us.
I tried to load a pickle with a trained "XGBoost" model in Python with the same command above. However, I received an error as:
>> fid=py.open('save_results.pkl','rb');
>> data = py.pickle.load(fid);
Error using libpath>find_lib_path (line 66)
Python Error: XGBoostLibraryNotFound: Cannot find XGBoost Library in the candidate path. List of candidates:
- Library\mingw-w64\bin\xgboost.dll
- C:\Users\20195127\Anaconda3\lib\site-packages\xgboost\../../windows/x64/Release/xgboost.dll
- C:\Users\20195127\Anaconda3\lib\site-packages\xgboost\./windows/x64/Release/xgboost.dll
XGBoost Python package path: C:\Users\20195127\Anaconda3\lib\site-packages\xgboost
sys.prefix:
See: https://xgboost.readthedocs.io/en/latest/build.html for installing XGBoost.
Error in core>_load_lib (line 133)
Error in core><module> (line 174)
Error in __init__><module> (line 9)
Do you know how to solve it?
Walter Roberson
le 22 Mar 2021
I notice that the first of those does not give a fully qualified path, which suggests you might have to be cd'd to a very specific location.... or could suggest that something is misconfigured in the paths
Have you searched your system for xgboost.dll ? Which directories did you find it in?
Quan minhquanccna
le 23 Mar 2021
Below directories that I can find with xgboost.dll:

I also follow this guide to install the xgboost and work with MATLAB. But the problem is still there.
Yun Guan
le 9 Nov 2021
I use the "data=py.pickle.load(fid)" to read my pckl data, but it show"Python error ModuleNotFoundError: No module named 'numpy'" , have you some idea? thanks
Dev Sarma
le 12 Mar 2022
You have to make sure the numpy package is installed. You can install numpy from your command line 'python -m pip install numpy'. (or py instead of python). More information here: https://numpy.org/install/
Jan
le 16 Mar 2018
0 votes
Did you take the time to search in the net before asking?
- https://xcorr.net/2013/06/12/load-pickle-files-in-matlab/
- https://stackoverflow.com/questions/25859968/in-matlab-how-to-read-python-pickle-file
Does this help already? I do not find a Matlab code to interpret these Python pickle files.
2 commentaires
kc
le 31 Déc 2019
these threds are not very useful sir, specially for ppl dnt know python
Walter Roberson
le 31 Déc 2019
The second link shows a few lines of Python needed to read the files and convert them to something usable by MATLAB.
The pickle format is python internal format that is not intended to be human readable, and is documented as not being compatible with other standards: https://docs.python.org/3/library/pickle.html
The data format used by pickle is Python-specific. This has the advantage that there are no restrictions imposed by external standards such as JSON or XDR (which can’t represent pointer sharing); however it means that non-Python programs may not be able to reconstruct pickled Python objects.
Thus you have python-specific datasets, and you would have a hard time reconstructing the data without using python.
See https://www.mathworks.com/help/matlab/call-python-libraries.html for information on calling python from MATLAB.
Catégories
En savoir plus sur Call Python from MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!