| 1 | function writebin(fnam,fld,typ,prec,skip) | 
| 2 |  | 
| 3 | % Function writebin(fnam,fld,typ,prec,skip) | 
| 4 | % write N-D binary field | 
| 5 | % | 
| 6 | % INPUTS | 
| 7 | % fnam  input path and file name | 
| 8 | % fld   input array | 
| 9 | % typ   0: sequential FORTRAN;  1: plain binary (the default) | 
| 10 | % prec  numeric precision (default 'real*4') | 
| 11 | % skip  records to skip before writing (default 0) | 
| 12 | % | 
| 13 | % SEE ALSO | 
| 14 | % readbin | 
| 15 |  | 
| 16 | if nargin < 5, skip=0; end | 
| 17 | if nargin < 4, prec='real*4'; end | 
| 18 | if nargin < 3, typ=1; end | 
| 19 | if skip>0 & typ==0, error('feature not implemented yet'); end | 
| 20 | if nargin < 2, error('please specify array and output file name'); end | 
| 21 | reclength=0; | 
| 22 |  | 
| 23 | if exist(fnam)==2 | 
| 24 | fid=fopen(fnam,'r+','ieee-be'); | 
| 25 | else | 
| 26 | fid=fopen(fnam,'w','ieee-be'); | 
| 27 | end | 
| 28 |  | 
| 29 | switch typ | 
| 30 |  | 
| 31 | case 0 | 
| 32 |  | 
| 33 | write_record(fid,fld,prec); | 
| 34 |  | 
| 35 | case 1 | 
| 36 |  | 
| 37 | if skip==0 | 
| 38 |  | 
| 39 | fwrite(fid,fld,prec); | 
| 40 |  | 
| 41 | else | 
| 42 |  | 
| 43 | switch prec | 
| 44 | case {'int8','integer*1'} | 
| 45 | reclength=prod(size(fld)); | 
| 46 | case {'int16','integer*2','uint16','integer*2'} | 
| 47 | reclength=2*prod(size(fld)); | 
| 48 | case {'int32','integer*4','uint32','single','real*4','float32'} | 
| 49 | reclength=4*prod(size(fld)); | 
| 50 | case {'int64','integer*8','uint64','double','real*8','float64'} | 
| 51 | reclength=8*prod(size(fld)); | 
| 52 | end | 
| 53 |  | 
| 54 | if fseek(fid,skip*reclength,'bof') == 0 | 
| 55 |  | 
| 56 | fwrite(fid,fld,prec); | 
| 57 |  | 
| 58 | else | 
| 59 |  | 
| 60 | fseek(fid,0,'eof'); | 
| 61 | file_length=ftell(fid); | 
| 62 | fwrite(fid,zeros((skip*reclength-file_length),1),'int8'); | 
| 63 | fwrite(fid,fld,prec); | 
| 64 |  | 
| 65 | end | 
| 66 |  | 
| 67 | end | 
| 68 |  | 
| 69 | end | 
| 70 |  | 
| 71 | fid=fclose(fid); |