How do read .npy files in matlab?

Hello.
In matlab it is possible to read the extension .npy, or in its absence the extension .LVM of Labview.

 Accepted Answer

Andrew Knyazev
Andrew Knyazev on 11 Jun 2019
Edited: Walter Roberson on 20 Dec 2024

5 Comments

I copied readNPY.m and readNPYheader.m from there and could then easily read npy-images into matlab.
Thankyou very much! /Maria
Excellent!! Thankyou very much...
Is it possible for you to show or briefly explain how to use this readNPY.m and readNPYheader.m/how to integrate this to our codes?
Super useful, thanks!
Best response from my point of view!

Sign in to comment.

More Answers (3)

If you have access to Python, the easiest thing would be to create a Python script such as the one below, and run that. It will find all .npz files in the current directory and convert that to .mat.
from scipy.io import savemat
import numpy as np
import glob
import os
npzFiles = glob.glob("*.npz")
for f in npzFiles:
fm = os.path.splitext(f)[0]+'.mat'
d = np.load(f)
savemat(fm, d)
print('generated ', fm, 'from', f)

7 Comments

i tried this, it seems to work but it's asking me to allow_pickle=True
In my case I had to do something like this
X = np.load('./dataset/dataset_Xs.npy')
savemat('./dataset/dataset_Xs.mat', {'X': X})
This works. Thanks Leonardo :)
Good job. Thanks.
Help me greatly thank u
thank you
"the easiest thing would be to create a Python script such as the one below, and run that."
This is easier:

Sign in to comment.

Grace Kepler
Grace Kepler on 7 Nov 2023
Edited: Stephen23 on 20 Dec 2024
Yes, use code like this.
x = py.numpy.load('data.npy')

3 Comments

Mike Jonson
Mike Jonson on 10 Sep 2024
Edited: Mike Jonson on 10 Sep 2024
No. That'd be how to do it with python, not in MATLAB.
Hi Mike,
Did you try it? This works for me. Note that "py." is prepended to the normal Python command "numpy.load('data.npy')".
>> x=py.numpy.load('data.npy')
x =
Python ndarray:
1 2 3
4 5 6
7 8 9

Sign in to comment.

Cam Salzberger
Cam Salzberger on 14 Feb 2019
Hello Juan,
I don't believe that either of those file formats are natively supported in MATLAB, but you can always check File Exchange to see if anyone has written an importer for them. A quick search did not turn up anything for NPY, but LVM turned up this one. If you have any questions about that File Exchange package, it's best to direct them to the author of the package.
-Cam

2 Comments

One more reason to stop using Matlab. Python indeed has libraries to make use Matlab.

Sign in to comment.

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!