%saves raw data into a matrix, across rows (TV raster scan order)
%
%Examples:
% saveraw('/mas/garden/flakes/steve/perspectivepics/red/data',A)
% saveraw('/mas/garden/flakes/steve/perspectivepics/red/data',A,'uchar')
%
%Types: char, schar,  short,  int,  long,  float, double,
%       uchar,        ushort, uint, ulong,
%       float32, float64, int8, int16, int32,
%       intN, uintN                               where 1 <= N <= 32
%
%overwrites file if already exists


function mycountreturned = easy_way_to_use_fwrite(myfilename,A,mytype);
if nargin==2
  mytype='uchar';
end%if
myint=fopen(myfilename,'w');
if myint<0
  error('saveraw file write failed')
end%if
mycount=fwrite(myint,A.',mytype);  % transpose for TV order
[M N]=size(A);
if mycount ~= M*N
  error(sprintf('saveraw: end of file reached; only %g of %g*%g=%g elements written',...
                mycount,M,N,M*N))
end%if

if nargout==1
  mycountreturned=mycount;
end%if
if nargout==0
  disp(sprintf(['saveraw: %g of %g*%g=%g elements written to ' myfilename],...
                mycount,M,N,M*N))
end%if
fclose_return=fclose(myint);   % returns zero if ok
if fclose_return ~= 0
  disp('saveraw: fclose returned nonzero value, so problem closing file');
end%if

