function WriteArray(array, filename) %WRITEARRAY Writes an array to a file. % WriteArray(array, filename) writes array to a file with the name "filename" % % The first four bytes (an integer) in the file indicate the number of dimensions of the array. % The size of the array along each of the dimensions are then written as four bytes each (integers), % note that the size along each of the dimensions must remain constant % The rest of the file contains the array values (double values) % % The 64-bit IEEE 754 floating-point "double format" bit layout is used % to write the byte representation of double values to the file. % % All values are written to the file in little endian form. % % If the file already exists, then it will be overwritten. % % See also ReadArray % Copyright (C) 2005 Vincent Cheung (vincent@psi.toronto.edu, http://www.psi.toronto.edu/~vincent/) % % This program is free software; you can redistribute it and/or % modify it under the terms of the GNU General Public License % as published by the Free Software Foundation; either version 2 % of the License, or (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % $Revision: 1.1 $ $Date: Sept. 17, 2004 $ % open the file to write (create if necessary) and as IEEE floating point with little-endian byte ordering fid = fopen(filename, 'w', 'l'); sz = size(array); numDimensions = length(sz); % write the number of dimensions and the size of the array long each dimension fwrite(fid, numDimensions, 'int32'); fwrite(fid, sz, 'int32'); % write the array values fwrite(fid, permute(array, numDimensions:-1:1), 'double'); fclose(fid);