-
Notifications
You must be signed in to change notification settings - Fork 0
/
readNPY.m
37 lines (27 loc) · 878 Bytes
/
readNPY.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function data = readNPY(filename)
% Function to read NPY files into matlab.
% *** Only reads a subset of all possible NPY files, specifically N-D arrays of certain data types.
% See https://github.com/kwikteam/npy-matlab/blob/master/tests/npy.ipynb for
% more.
%
[shape, dataType, fortranOrder, littleEndian, totalHeaderLength, ~] = readNPYheader(filename);
if littleEndian
fid = fopen(filename, 'r', 'l');
else
fid = fopen(filename, 'r', 'b');
end
try
[~] = fread(fid, totalHeaderLength, 'uint8');
% read the data
data = fread(fid, prod(shape), [dataType '=>' dataType]);
if length(shape)>1 && ~fortranOrder
data = reshape(data, shape(end:-1:1));
data = permute(data, [length(shape):-1:1]);
elseif length(shape)>1
data = reshape(data, shape);
end
fclose(fid);
catch me
fclose(fid);
rethrow(me);
end