Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read .npy files in Matlab

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.

like image 520
John Manak Avatar asked Feb 04 '14 13:02

John Manak


People also ask

What are .NPY files?

What is this . NPY file? It is a standard binary file format for persisting a single arbitrary NumPy array on a disk. The format stores all of the shape and data type information necessary to reconstruct the array correctly even on another machine with a different architecture.

Why you should start using .NPY file more often?

Saving the NumPy data into . npy files increases the efficiency of saving and loading data as it is stored in a native binary format. Using . npy files is common for when a particular data set needs to be cleaned, transformed and prepared to be used in the testing of a machine learning model in the future.


3 Answers

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');
like image 114
miluz Avatar answered Sep 20 '22 11:09

miluz


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

like image 30
Daniel Avatar answered Sep 20 '22 11:09

Daniel


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)

like image 30
M.Hossein Rahimi Avatar answered Sep 18 '22 11:09

M.Hossein Rahimi