Method to Read NPY Data Saved by Python in MATLAB

Method to Read NPY Data Saved by Python in MATLAB

The reason for this issue is that there are many programming languages, and sometimes data processing or plotting is done interactively!

Whether it is used or not, who can say for sure after learning more!

1. The simplest and easiest to understand

Directly convert using Python

From .npy 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)

The second simple MATLAB code, but requires downloading a function package

npy-matlab

You can click to read the original text to download the function package:

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

Usage:

Installation

After downloading the zip file or git of npy-matlab, simply add the NPY-Matlab directory to your search path:

>> addpath('my-idiosyncratic-path/npy-matlab/npy-matlab')   >> savepath

Usage Example

>> a= rand(5,4,3); >> writeNPY(a, 'a.npy'); >> b= readNPY('a.npy'); >> sum(a(:)==b(:)) ans =60

Sharing my tested and used experience! Definitely thumbs up 👍

Leave a Comment