1 |
% WRDA( file, arr, irec [options] ) |
2 |
% |
3 |
% This routine writes the array 'arr' as a record in a direct-access |
4 |
% binary file (float or double precision) with filename 'file'. |
5 |
% The data will be written as the irec'th record. The file is created |
6 |
% if it does not exists. |
7 |
% |
8 |
% Required arguments: |
9 |
% |
10 |
% file - string - name of file to create/append |
11 |
% arr - array - matlab matrix or multi-dimensional array |
12 |
% irec - integer - record number in file in which to write data |
13 |
% |
14 |
% Optional arguments (must appear after the required arguments): |
15 |
% prec - string - precision of storage in file. Default = 'real*8'. |
16 |
% ieee - string - IEEE bit-wise representation in file. Default = 'b'. |
17 |
% |
18 |
% 'prec' may take the values: |
19 |
% 'real*4' - floating point, 32 bits. |
20 |
% 'real*8' - floating point, 64 bits - the default. |
21 |
% |
22 |
% 'ieee' may take values: |
23 |
% 'ieee-be' or 'b' - IEEE floating point with big-endian |
24 |
% byte ordering - the default |
25 |
% 'ieee-le' or 'l' - IEEE floating point with little-endian |
26 |
% byte ordering |
27 |
% 'native' or 'n' - local machine format |
28 |
% 'cray' or 'c' - Cray floating point with big-endian |
29 |
% byte ordering |
30 |
% 'ieee-le.l64' or 'a' - IEEE floating point with little-endian |
31 |
% byte ordering and 64 bit long data type |
32 |
% 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte |
33 |
% ordering and 64 bit long data type. |
34 |
% |
35 |
% eg. wrda('t.data',T,4); |
36 |
% wrda('t.data',T,4,'real*4'); |
37 |
% wrda('t.data',T,4,'real*4','b'); |
38 |
function [] = wrda(file,arr,k,varargin) |
39 |
|
40 |
% Defaults |
41 |
WORDLENGTH=8; |
42 |
rtype='real*8'; |
43 |
ieee='b'; |
44 |
|
45 |
% Check optional arguments |
46 |
args=char(varargin); |
47 |
while (size(args,1) > 0) |
48 |
if deblank(args(1,:)) == 'real*4' |
49 |
WORDLENGTH=4; |
50 |
rtype='real*4'; |
51 |
elseif deblank(args(1,:)) == 'real*8' |
52 |
WORDLENGTH=8; |
53 |
rtype='real*8'; |
54 |
elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' |
55 |
ieee='n'; |
56 |
elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' |
57 |
ieee='l'; |
58 |
elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' |
59 |
ieee='b'; |
60 |
elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' |
61 |
ieee='c'; |
62 |
elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' |
63 |
ieee='a'; |
64 |
elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' |
65 |
ieee='s'; |
66 |
else |
67 |
sprintf(['Optional argument ' args(1,:) ' is unknown']) |
68 |
return |
69 |
end |
70 |
args=args(2:end,:); |
71 |
end |
72 |
|
73 |
N=size(arr); |
74 |
nnn=prod(N); |
75 |
|
76 |
[fid mess]=fopen(file,'r+',ieee); |
77 |
if fid == -1 |
78 |
[fid mess]=fopen(file,'w',ieee); |
79 |
if fid == -1 |
80 |
sprintf('Error while opening file:\n%s',mess) |
81 |
return |
82 |
end |
83 |
end |
84 |
st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof'); |
85 |
if st ~= 0 |
86 |
mess=ferror(fid); |
87 |
sprintf('There was an error while positioning the file pointer:\n%s',mess) |
88 |
return |
89 |
end |
90 |
count=fwrite(fid,arr,rtype); |
91 |
if count ~= nnn |
92 |
sprintf('Not enough data was available to be read: off EOF?') |
93 |
return |
94 |
end |
95 |
st=fclose(fid); |