Home > analyzePRF > utilities > loadbinary.m

loadbinary

PURPOSE ^

function m = loadbinary(file,precision,msize,lastdimrange,dim)

SYNOPSIS ^

function m = loadbinary(file,precision,msize,lastdimrange,dim)

DESCRIPTION ^

 function m = loadbinary(file,precision,msize,lastdimrange,dim)

 <file> is a pattern matching one or more files (see matchfiles.m)
 <precision> is something like 'int16'
 <msize> (optional) is the expected dimensions of the matrix for one file.
   one of the dimensions can be 0, in which case we figure out what
   that number should be.  default: [1 0].
 <lastdimrange> (optional) is
   [A B] where A<=B and A and B are indices referring to the last dimension 
         of <m> (i.e. the last entry in <msize>).  this indicates that we
         want the portion of the matrix that lies between A to B (inclusive.)
   -V where V is a vector of indices referring to the last dimension of <m>.
      this indicates that we want exactly the indices specified by V.  V can
      have indices in any order and may include repeats. 
   we read and return only the portion of the matrix specified by
   <lastdimrange>.  default is [], which means return the whole matrix.
 <dim> (optional) is the dimension along which to concatenate 
   matrices from different files.  default: 1.

 read <file> and return a matrix.  we assume that each file contains
 some data (i.e. it isn't empty).  for the machine format, we assume 
 IEEE floating point with little-endian byte ordering ('l'; see fopen).

 see also savebinary.m.

 example:
 savebinary('test','uint8',repmat(0:255,[2 1]));
 isequal(loadbinary('test','uint8',[0 256],[255 256]),repmat([254 255],[2 1]))

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function m = loadbinary(file,precision,msize,lastdimrange,dim)
0002 
0003 % function m = loadbinary(file,precision,msize,lastdimrange,dim)
0004 %
0005 % <file> is a pattern matching one or more files (see matchfiles.m)
0006 % <precision> is something like 'int16'
0007 % <msize> (optional) is the expected dimensions of the matrix for one file.
0008 %   one of the dimensions can be 0, in which case we figure out what
0009 %   that number should be.  default: [1 0].
0010 % <lastdimrange> (optional) is
0011 %   [A B] where A<=B and A and B are indices referring to the last dimension
0012 %         of <m> (i.e. the last entry in <msize>).  this indicates that we
0013 %         want the portion of the matrix that lies between A to B (inclusive.)
0014 %   -V where V is a vector of indices referring to the last dimension of <m>.
0015 %      this indicates that we want exactly the indices specified by V.  V can
0016 %      have indices in any order and may include repeats.
0017 %   we read and return only the portion of the matrix specified by
0018 %   <lastdimrange>.  default is [], which means return the whole matrix.
0019 % <dim> (optional) is the dimension along which to concatenate
0020 %   matrices from different files.  default: 1.
0021 %
0022 % read <file> and return a matrix.  we assume that each file contains
0023 % some data (i.e. it isn't empty).  for the machine format, we assume
0024 % IEEE floating point with little-endian byte ordering ('l'; see fopen).
0025 %
0026 % see also savebinary.m.
0027 %
0028 % example:
0029 % savebinary('test','uint8',repmat(0:255,[2 1]));
0030 % isequal(loadbinary('test','uint8',[0 256],[255 256]),repmat([254 255],[2 1]))
0031 
0032 % constants
0033 machineformat = 'l';
0034 
0035 % input
0036 if ~exist('msize','var') || isempty(msize)
0037   msize = [1 0];
0038 end
0039 if ~exist('lastdimrange','var') || isempty(lastdimrange)
0040   lastdimrange = [];
0041 end
0042 if ~exist('dim','var') || isempty(dim)
0043   dim = 1;
0044 end
0045 
0046 % massage input
0047 if ~isempty(lastdimrange)
0048   if any(lastdimrange < 0)
0049     lastdimrange = -lastdimrange;
0050   else
0051     lastdimrange = lastdimrange(1):lastdimrange(2);
0052   end
0053 end
0054 
0055 % get file name
0056 file = matchfiles(file);
0057 assert(length(file) >= 1,'<file> does not match at least one file');
0058 
0059 % loop through files
0060 for p=1:length(file)
0061 
0062   % open file
0063   fid = fopen(file{p},'r',machineformat);
0064   assert(fid ~= -1,'<file> could not be opened for reading');
0065   
0066   % handle case of no specific range
0067   if isempty(lastdimrange)
0068   
0069     % read it all in
0070     m0 = fread(fid,Inf,['*' precision],0,machineformat);
0071   
0072     % figure out msize
0073     if any(msize==0)
0074       assert(sum(msize==0)==1);  % make sure only one is 0
0075       msize(msize==0) = prod(size(m0))/prod(msize(msize~=0));  % calc appropriate value
0076       assert(all(isint(msize)),'<msize> is not correct');
0077     end
0078   
0079     % reshape
0080     m0 = reshape(m0,msize);
0081   
0082   % handle case of specific range
0083   else
0084   
0085     % peek to find out the byte size of a word
0086     temp = fread(fid,1,['*' precision],0,machineformat);
0087     wordsize = getfield(whos('temp'),'bytes');
0088     
0089     % check how big the data segment is
0090     assert(fseek(fid,0,'bof')==0);
0091     pos1 = ftell(fid); assert(pos1~=-1);
0092     assert(fseek(fid,0,'eof')==0);
0093     pos2 = ftell(fid); assert(pos2~=-1);
0094     
0095     % calculate number of words
0096     numwords = (pos2-pos1)/wordsize; assert(isint(numwords));
0097     
0098     % figure out msize
0099     if any(msize==0)
0100       assert(sum(msize==0)==1);  % make sure only one is 0
0101       msize(msize==0) = numwords/prod(msize(msize~=0));  % calc appropriate value
0102       assert(all(isint(msize)),'<msize> is not correct');
0103     end
0104     
0105     % calc slice size in terms of number of words
0106     slicesize = prod(msize(1:end-1));
0107     
0108     % ok, now we have to do fancy handling to deal with arbitrary vectors of indices
0109     
0110     % process chunks of consecutive indices
0111     lastdimrange_sorted = sort(lastdimrange);
0112     cur = 1;  % pos in sorted list that we are on currently
0113     m0 = cast([],precision);  % initialize
0114     while cur <= length(lastdimrange_sorted)
0115       ff = find(diff(lastdimrange_sorted(cur:end))~=1);  % ff(1) tells us how many consecutive integers we have
0116       if isempty(ff)  % in this case, the entire list is consecutive integers
0117         rng = [lastdimrange_sorted(cur) lastdimrange_sorted(end)];
0118         cur = cur + diff(rng)+1;
0119       else
0120         rng = [lastdimrange_sorted(cur) lastdimrange_sorted(cur)+ff(1)-1];
0121         cur = cur + ff(1);
0122       end
0123       
0124       % calc number of slices wanted
0125       numslices = rng(2)-rng(1)+1;
0126     
0127       % read data and reshape
0128       assert(fseek(fid,slicesize*(rng(1)-1)*wordsize,'bof')==0);
0129       m0 = cat(length(msize),m0,reshape(fread(fid,numslices*slicesize,['*' precision],0,machineformat),[msize(1:end-1) numslices]));
0130     end
0131     
0132     % now, return exactly what the user wanted
0133     m0 = subscript(m0,[repmat({':'},[1 length(msize)-1]) {calcposition(lastdimrange_sorted,lastdimrange)}]);
0134     
0135   end
0136   
0137   % close file
0138   assert(fclose(fid)==0);
0139   
0140   % save
0141   if p==1
0142     m = m0;  % get the datatype right instead of initializing to double via []
0143   else
0144     m = cat(dim,m,m0);
0145   end
0146 
0147 end

Generated on Wed 18-Jun-2014 21:47:41 by m2html © 2005