Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion constructNPYheader.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

for s = 1:length(shape)
dictString = [dictString num2str(shape(s))];
if s<length(shape)
if s<length(shape) || s==1
dictString = [dictString ', '];
end
end
Expand Down
32 changes: 32 additions & 0 deletions writeNPYshaped.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


function writeNPYshaped(var, filename, shape)
% function writeNPY(var, filename, shape)
%
% Only writes little endian, fortran (column-major) ordering; only writes
% with NPY version number 1.0.
%
% shape is the size of the array as in the NPY file. You need this to
% override matlab's convention, e.g. (10, 1)rather than (10,).
% if not specified, defaults to matlab's size(var)

if nargin<3
shape = size(var);
end

if prod(shape)~=prod(size(var))
error('shape input does not matach number of elements in var')
end

dataType = class(var);

header = constructNPYheader(dataType, shape);

fid = fopen(filename, 'w');
fwrite(fid, header, 'uint8');
fwrite(fid, var, dataType);
fclose(fid);


end