15

I was wondering if there is way to read .npy files in Matlab? I know I can convert those to Matlab-style .mat files using scipy.io.savemat in Python; however I'm more interested in a native or plugin support for .npy files in Matlab.

YXD
  • 31,741
  • 15
  • 75
  • 115
John Manak
  • 13,328
  • 29
  • 78
  • 119
  • 2
    The file format specification is [here](https://github.com/numpy/numpy/blob/master/doc/neps/npy-format.txt). I doubt that someone has written a Matlab routine for reading this format, but I may be wrong. – pv. Feb 04 '14 at 14:11
  • what about `.npz` files? – Charlie Parker Oct 14 '16 at 22:47

3 Answers3

17

This did the job for me, I used it to read npy files.

https://github.com/kwikteam/npy-matlab

If you only want to read .npy file all you need from the npy-matlab project are two files: readNPY.m and readNPYheader.m.

Usage is as simple as:

>> im = readNPY('/path/to/file.npy');
miluz
  • 1,353
  • 3
  • 14
  • 22
  • 1
    This may theoretically answer the question, but it would be best to include the essential parts of the answer here for future users, and provide the link for reference. [Link-dominated answers](//meta.stackexchange.com/questions/8231) can become invalid through [link rot](//en.wikipedia.org/wiki/Link_rot). – Mogsdad Jan 11 '16 at 14:34
  • 1
    Actually the code is licensed and could not also be posted here. On the other hand readNPY is not too long to kind of explain it. Difficult to say what is the best here. – NoDataDumpNoContribution Jan 11 '16 at 22:30
  • @Trilarion - There is no need to post the licensed code here, and that's not what we're looking for. The revised answer is good (+1 for taking the effort to improve it, miluz), as it now shows a simple usage example, whereas the original consisted only of a recommendation and a link. (IMHO the accepted answer is just barely OK, wrt quality, if you wanted to compare.) – Mogsdad Jan 12 '16 at 15:04
  • @Mogsdad Are we not? Isn't the rationale that content of links may die at some point. What to do then with this answer? – NoDataDumpNoContribution Jan 12 '16 at 20:13
  • The answer starting from "If you only want...", is a self-contained answer, without the link. The link makes it more helpful. If the link dies, the answer is less helpful, but still an answer. (Just read the reference I provided in the first place wrt [Link-dominated answers](http://meta.stackexchange.com/questions/8231) for a full debate.) We're not the first to travel this rocky path... – Mogsdad Jan 12 '16 at 20:51
  • what about `.npz` files? – Charlie Parker Oct 14 '16 at 22:48
5

There is a c++ library available https://github.com/rogersce/cnpy

You could write a mex function to read the data. I would prefer to store everything in hdf5

Daniel
  • 36,610
  • 3
  • 36
  • 69
3

A quick way would be to read it in python, as below,

data = np.load('/tmp/123.npz')

Then save it as '.csv', again by python, using python documentation or,

numpy.savetxt('FileName.csv', arrayToSave)

(more documentation here)

Finally, you can read it in MATLAB using the following command,

csvread()

Quick Update: As the user "Ender" mentioned in the comments, the csvread() is now deprecated and readmatrix() stands in its lieu. (documentation)

M.Hossein Rahimi
  • 557
  • 2
  • 9
  • 20