function [array] = ReadArray(filename) %READARRAY Reads an array from a file. % array = ReadArray(filename) reads an array from the file pointed to by "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. % % See also WriteArray % 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.0 $ $Date: March 10, 2004 $ % open the file as read-only and as IEEE floating point with little-endian byte ordering fid = fopen(filename, 'r', 'l'); numDimensions = fread(fid, 1, 'int32'); sz = fread(fid, numDimensions, 'int32')'; array = fread(fid, prod(sz), 'double'); % reshape the array if(numDimensions > 1) array = permute(reshape(array, sz(end:-1:1)), [numDimensions:-1:1]); end fclose(fid);