📄 gaussread.m
字号:
disp (' ')
errorFlag = 1;
end
elseif data_type == hex2dec('8086') % COMPLEX Matrix.
[real_data , count] = fread(fid , (rows * columns) , 'double');
[imag_data , count] = fread(fid , (rows * columns) , 'double');
if (rows * columns) == count
data = reshape((real_data + j*imag_data) , columns , rows).';
else
disp (' ')
disp (' ERROR: COMPLEX Matrix Inconsistency between Header/Body Information.')
disp (' The number of matrix elements expected from the header ')
disp (' does NOT match the number of elements actually read. ')
disp (' ')
errorFlag = 1;
end
else % Invalid Matrix Type or Inconsistent Input Data Dimensions.
disp (' ')
disp (' ERROR: Invalid Matrix Type/Input File (Neither REAL nor COMPLEX)')
disp (' ')
errorFlag = 1;
end
%
% * * * * * * * * * End of readFMT * * * * * * * * *
%
function [data,varNames,errorFlag] = readDHT(fDHT , fDAT , fileFormat)
%READDHT Read GAUSS binary data set files (paired files with .DHT/.DAT extensions).
%
% [Data , VarNames , ErrorFlag] = readFMT(FDHT , FDAT , FileFormat)
%
% READDHT reads binary GAUSS data set files created with the GAUSS command
% CREATE or SAVED. The CREATE command will generate a pair of files: The
% first is a descriptor file with .DHT extension that stores header and variable
% name information; the second file stores a matrix in either 2-byte signed
% integer, 4-byte single-precision, or 8-byte double precision. Note that the
% .DHT descriptor file extension must NOT change, but the .DAT data file
% extension may be changed by the user.
%
% Inputs:
% FDHT: File handle of the GAUSS binary data set descriptor (.DHT) file to read.
%
% FDAT: File handle of the GAUSS binary data set (.DAT) file associated with
% the .DHT descriptor file.
%
% FileFormat: Type of data set binary files: 'DDDD' HEX is a simple data set
% file format; 'EEDA' HEX extended data set file format.
%
% Outputs:
% Data: Contents of GAUSS .DAT data set file converted to a MATLAB matrix.
%
% VarNames: Cell array of variable names associated with each of the columns
% of the GAUSS data set.
%
% ErrorFlag: Error flag indicator = 0 if no error occurrs and = 1 if an
% error occurred trying to read/decode the binary matrix file.
%
% Author(s): R.A. Baker, 07/23/98
errorFlag = 0; % Intialize to 'No Error' condition.
data = []; % Intialize output matrix placeholder.
varNames = '';
if fileFormat == hex2dec('DADA') % Simple dataset file format.
%
% Read the data set header information:
%
% Bytes 0-1 = DADA HEX identification flag
% Bytes 2-5 = Reserved (zero padded)
% Bytes 6-7 = Number of columns (unsigned 2-byte integer)
% Bytes 8-9 = Rows size in bytes (unsigned 2-byte integer)
% Bytes 10-11 = Header size in bytes (unsigned 2-byte integer)
% Bytes 12-13 = Data type in corresponding .DAT file {2,4,8}: (unsigned 2-byte integer)
% Bytes 14-17 = Reserved (zero padded)
% Bytes 18-21 = Reserved (zero padded)
% Bytes 22-23 = Control flags: {0 = Real Data, 1 = Complex} (unsigned 2-byte integer)
% Bytes 24-127 = Reserved (zero padded)
%
[header , count] = fread(fDHT , 4 , 'uint8'); % Reserved zeros.
[header , count] = fread(fDHT , 4 , 'uint16');
columns = header(1); % Number of columns.
rowSize = header(2); % Row size in bytes.
headerSize = header(3); % Header size in bytes.
dataPrecision = header(4); % Data precision {2 = integer , 4 = single , 8 = double}.
[header , count] = fread(fDHT , 8 , 'uint8'); % Reserved zeros.
[dataType, count] = fread(fDHT , 1 , 'uint16'); % Real = '0000'(BCD) = 0 , Complex = '0100' (BCD) = 4.
[header , count] = fread(fDHT , 104 , 'uint8'); % Reserved zeros.
%
% Read the column names associated with the data set into a cell array.
%
[header , count] = fread(fDHT , [8,columns] , 'uchar');
varNames = cellstr(deblank(char(header')));
elseif fileFormat == hex2dec('EEDA') % Extended dataset file format.
%
% Read the data set header information:
%
% Bytes 0-1 = EEDA HEX identification flag
% Bytes 2-3 = Data type in corresponding .DAT file {2,4,8}: (unsigned 2-byte integer)
% Bytes 4-7 = Reserved (zero padded)
% Bytes 8-11 = Number of columns (unsigned 4-byte integer)
% Bytes 12-15 = Rows size in bytes (unsigned 4-byte integer)
% Bytes 16-19 = Header size in bytes (unsigned 4-byte integer)
% Bytes 20-23 = Reserved (zero padded)
% Bytes 24-27 = Reserved (zero padded)
% Bytes 28-29 = Control flags: {0 = Real Data, 1 = Complex} (unsigned 2-byte integer)
% Bytes 30-127 = Reserved (zero padded)
%
[dataPrecision, count] = fread(fDHT , 1 , 'uint16'); % Data precision {2 = integer , 4 = single , 8 = double}.
[header , count] = fread(fDHT , 4 , 'uint8'); % Reserved zeros.
[header , count] = fread(fDHT , 3 , 'uint32');
columns = header(1); % Number of columns.
rowSize = header(2); % Row size in bytes.
headerSize = header(3); % Header size in bytes.
[header , count] = fread(fDHT , 8 , 'uint8'); % Reserved zeros.
[dataType, count] = fread(fDHT , 1 , 'uint16'); % Real = '0000'(BCD) = 0 , Complex = '0100' (BCD) = 4.
[header , count] = fread(fDHT ,98 , 'uint8'); % Reserved zeros.
%
% Read the column names associated with the data set into a cell array.
%
[header , count] = fread(fDHT , [8,columns] , 'uchar');
varNames = cellstr(deblank(char(header')));
end
%
% Read the data & resize the matrix if data consistency exists between the header & body.
%
if dataType == 0 % REAL Data Set.
count = 0;
if dataPrecision == 2
[data , count] = fread(fDAT , inf , 'int16');
elseif dataPrecision == 4
[data , count] = fread(fDAT , inf , 'single');
elseif dataPrecision == 8
[data , count] = fread(fDAT , inf , 'double');
end
rows = count / columns;
if round(rows) == rows
data = reshape(data , columns , rows)';
else
disp (' ')
disp (' ERROR: REAL Matrix Inconsistency between Header/Body Information.')
disp (' The number of matrix elements expected from the header ')
disp (' does NOT match the number of elements actually read. ')
disp (' ')
errorFlag = 1;
end
elseif dataType == 4 % COMPLEX Data Set.
count = 0;
if dataPrecision == 2
[data , count] = fread(fDAT , inf , 'int16');
elseif dataPrecision == 4
[data , count] = fread(fDAT , inf , 'single');
elseif dataPrecision == 8
[data , count] = fread(fDAT , inf , 'double');
end
data = [data(1:2:count-1) data(2:2:count)]; % Put real part in column 1, imaginary part in column 2.
rows = count / columns / 2; % Compute # of rows in the matrix.
if (round(rows) - rows) == 0
data = reshape((data(:,1) + j*data(:,2)) , columns , rows).';
else
disp (' ')
disp (' ERROR: COMPLEX Matrix Inconsistency between Header/Body Information.')
disp (' The number of matrix elements expected from the header ')
disp (' does NOT match the number of elements actually read. ')
disp (' ')
errorFlag = 1;
end
else % Invalid Matrix Type or Inconsistent Input Data Dimensions
disp (' ')
disp (' ERROR: Invalid Matrix Type/Input File (Neither REAL nor COMPLEX)')
disp (' ')
errorFlag = 1;
end
%
% * * * * * * * * * End of readDHT * * * * * * * * *
%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -