MATLAB Answers

How can i open dataset in matlab which is .pkl format?

469 views (last 30 days)
ambily c
ambily c on 16 Mar 2018
Commented: kc on 21 Jan 2020
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 Comment

kc
kc on 31 Dec 2019
Is there any way, i am also gt stuck here.
plz help

Sign in to comment.

Answers (2)

Tim R
Tim R on 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);

  3 Comments

kc
kc on 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
Walter Roberson on 21 Jan 2020
filename = fileread('NameOfFileThatHasPathToDataFile');
fid=py.open(filename,'rb');
data=py.pickle.load(fid);
kc
kc on 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 :)

Sign in to comment.


Jan
Jan on 16 Mar 2018
Did you take the time to search in the net before asking?
Does this help already? I do not find a Matlab code to interpret these Python pickle files.

  2 Comments

kc
kc on 31 Dec 2019
these threds are not very useful sir, specially for ppl dnt know python
Walter Roberson
Walter Roberson on 31 Dec 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!