forked from nputs/Gannet2.0_nicksversion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fwriteVAXD.m
executable file
·62 lines (52 loc) · 2.04 KB
/
fwriteVAXD.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function count = fwriteVAXD(fid, A, precision)
% FWRITEVAXD(FID, A , PRECISION) writes the elements of 'A' to the
% specified file in VAXD format, translating MATLAB values to the specified
% precision. COUNT is the number of elements successfully written.
%
% FID is an integer file identifier obtained from FOPEN. FWRITEVAXD requires
% FOPEN to open the output file in IEEE little-endian machineformat.
%
% PRECISION controls the form and size of result. See FREAD for supported
% formats.
%
% Usage:
% A = rand(3,3);
% fid = fopen('myFile', 'w', 'ieee-le');
% count = fwriteVAXD(fid, A, 'double');
% fclose(fid);
%
% The function is intended to be called by the user.
%
% 2009 The MathWorks, Inc. MATLAB and Simulink are registered trademarks
% of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of
% additional trademarks. Other product or brand names may be trademarks or
% registered trademarks of their respective holders.
% Check for proper number of input arguments
if nargin < 2
error('Not enough input arguments.')
end
% Check that the file has been open in ieee-le machineformat
[filename, permission, machineformat] = fopen(fid);
if ~strcmp(machineformat, 'ieee-le')
error('Use FOPEN with ieee-le precision');
end
switch precision
case {'float32', 'single'}
rawUINT32 = VAXF_to_uint32le(A);
count = fwrite(fid, rawUINT32, 'uint32');
case {'float64', 'double'}
rawUINT32 = VAXD_to_uint64le(A);
count = fwrite(fid, rawUINT32, 'uint32');
count = count/2;%2 UINT32 pieces for each double precision number
case {'float'}
if intmax == 2147483647 %32bit OS float is 32 bits
rawUINT32 = VAXF_to_uint32le(A);
count = fwrite(fid, rawUINT32, 'uint32');
else
rawUINT32 = VAXD_to_uint64le(A);
count = fwrite(fid, rawUINT32, 'uint32');
count = count/2;%2 UINT32 pieces for each double precision number
end
otherwise
count = fwrite(fid, A, precision, 'vaxd');
end