writeNPY.m 549 B

1234567891011121314151617181920212223
  1. % NPY-MATLAB writeNPY function. Copied from https://github.com/kwikteam/npy-matlab
  2. function writeNPY(var, filename)
  3. % function writeNPY(var, filename)
  4. %
  5. % Only writes little endian, fortran (column-major) ordering; only writes
  6. % with NPY version number 1.0.
  7. %
  8. % Always outputs a shape according to matlab's convention, e.g. (10, 1)
  9. % rather than (10,).
  10. shape = size(var);
  11. dataType = class(var);
  12. header = constructNPYheader(dataType, shape);
  13. fid = fopen(filename, 'w');
  14. fwrite(fid, header, 'uint8');
  15. fwrite(fid, var, dataType);
  16. fclose(fid);
  17. end