📄 gaussread.m
字号:
function [data,varNames] = gaussread(FMT_DHT_FileName , DAT_FileName)
%GAUSSREAD Import GAUSS binary files.
% Read either of two common GAUSS binary file formats:
% (1) Binary GAUSS matrix files created with the GAUSS command SAVE. The
% SAVE command will generate a file with a .FMT file extension, in which
% a single matrix is stored in double-precision.
% (2) Binary GAUSS data set files created with the GAUSS command CREATE or
% SAVED. The CREATE commands 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.
%
% [DataMatrix , VarNames] = gaussread(FMT_DHT_FileName)
% [DataMatrix , VarNames] = gaussread(FMT_DHT_FileName , DAT_FileName)
%
% Optional Input: DAT_FileName
%
% Input:
% FMT_DHT_FileName - String containing the name of the binary GAUSS matrix
% file (usually .FMT extension), or data set descriptor file (.DHT
% extension) to read. If the file resides in a directory that is not on
% the MATLAB path, then this string must include the full path of the file
% (e.g., 'C:\mydata\datasets\DATA.DHT').
%
% Optional Input:
% DAT_FileName - String containing the name of the binary GAUSS data set
% file (probably .DAT extension) associated with the .DHT descriptor file
% specified above in 'FMT_DHT_FileName'. If the file resides in a directory
% that is not on your MATLAB path, then this string must also include the
% full path of the file (e.g., 'C:\mydata\datasets\DATA.DAT').
%
% Outputs:
% DataMatrix - A MATLAB matrix containing the contents of a GAUSS matrix or
% data set file.
%
% VarNames - Cell array of variable names associated with each of the columns
% of the GAUSS data set. Valid ONLY for GAUSS data set files.
%
% Notes:
% (1) If the required 'FMT_DHT_FileName' file is a GAUSS matrix file, then
% 'DAT_FileName' is unnecessary and will be ignored even if specified.
% (2) If the required 'FMT_DHT_FileName' input file is a GAUSS data set
% descriptor (.DHT) file, then 'DAT_FileName' will be opened and read
% if it is specified. If 'DAT_FileName' is unspecified, then the .DAT
% file associated with the .DHT descriptor file will be constructed from
% the .DHT file by replacing .DHT extension with .DAT, thus retaining the
% same filename. For example, if 'DAT_FileName' is not specified and
% FMT_DHT_FileName = 'mydata.dht', then DAT_FileName = 'mydata.dat'.
%
% See also PATH, FREAD, FOPEN.
% Copyright 1999-2002 The MathWorks, Inc.
% $Revision: 1.7 $ $ Date: 1998/01/30 13:45:34 $
%
% Open the specified GAUSS binary file.
%
fid = fopen(FMT_DHT_FileName , 'r');
if fid == -1
error(sprintf(' Cannot open specified ''%s'' GAUSS file.\n' , FMT_DHT_FileName))
end
%
% Read the first 2 bytes of the header to determine the type of file:
%
% 'DDDD' HEX = Simple Matrix File Format (.FMT matrix with <= 8190 elements)
% 'EEDD' HEX = Extended Matrix File Format (.FMT matrix with > 8190 elements)
%
% 'DADA' HEX = Simple Data Set File Format (.DHT data set with <= 8175 columns)
% 'EEDA' HEX = Extended Data Set File Format (.DHT data set with > 8175 columns)
%
% Reference: GAUSS Volume I: System & Graphics Manual, Chapter 9, page 127-138.
%
[fileFormat , count] = fread(fid , 1 , 'uint16');
%
% Call the appropriate decoding function for this type of binary GAUSS file.
%
switch fileFormat
case {hex2dec('DDDD') , hex2dec('EEDD')} % FMT matrix files.
[data,errorFlag] = readFMT(fid , fileFormat);
fclose(fid);
varNames = '';
if errorFlag
error (' Problem reading GAUSS binary matrix (FMT) file.')
end
case {hex2dec('DADA') , hex2dec('EEDA')} % DHT/DAT data set files.
if nargin == 1
%
% Allow the convenience of NOT specifying the GAUSS data (.DAT) file
% associated with the descriptor (.DHT) file. If the data file is NOT
% specified, assume it has the same name and is in the same directory
% as the descriptor file, except replace 'DHT' extension with 'DAT'.
%
DAT_FileName = [strtok(FMT_DHT_FileName , '.') '.dat'];
fDAT = fopen(DAT_FileName , 'r');
if fDAT == -1
error(' Cannot open GAUSS data set .DAT file')
end
else
fDAT = fopen(DAT_FileName , 'r');
if fDAT == -1
fclose(fid);
error(sprintf(' Cannot open specified ''%s'' GAUSS data set DAT file.\n' , DAT_FileName))
end
end
[data,varNames,errorFlag] = readDHT(fid , fDAT , fileFormat);
fclose(fid);
fclose(fDAT);
if errorFlag
error (' Problem reading GAUSS data set (DHT/DAT) files.')
end
otherwise
fclose(fid);
error (' Invalid File Type: Neither matrix (FMT) nor data set descriptor (DHT) formats.')
end
%
% * * * * * * * * * End of gauss2ML * * * * * * * * *
%
function [data,errorFlag] = readFMT(fid , fileFormat)
%READFMT Read GAUSS binary matrix files (.FMT extension).
%
% [Data , ErrorFlag] = readFMT(FID , FileFormat)
%
% READFMT reads GAUSS binary matrix files created with the GAUSS command
% SAVE. The SAVE command will generate a file with a .FMT default file
% extension, in which a single matrix is stored in double-precision.
% The .FMT file extension may be changed by the user, so interpret any
% references to .FMT as a GAUSS binary matrix file.
%
% Inputs:
% FID: File handle of the GAUSS binary matrix (.FMT) file to read. Note
% that the main function GAUSS2ML has already opened the matrix file.
%
% FileFormat: Type of .FMT matrix binary file: 'DDDD' HEX is a simple
% matrix file format; 'EEDD' HEX extended matrix file format.
%
% Output:
% Data: Contents of GAUSS .FMT matrix file converted to a MATLAB matrix.
%
% ErrorFlag: Error flag indicator = 0 if no error occurrs and = 1 if an
% error occurred trying to read/decose the binary matrix file.
%
errorFlag = 0; % Intialize to 'No Error' condition.
data = []; % Intialize output matrix placeholder.
if fileFormat == hex2dec('DDDD') % Simple matrix file format.
%
% Read the matrix header information contained in the first 16-bytes. Each field is
% an unsigned 2-byte integer:
%
% Bytes 0-1 = DDDD HEX identification flag
% Bytes 2-3 = Number of rows
% Bytes 4-5 = Number of columns
% Bytes 6-7 = Size of the file minus the 16-byte header
% Bytes 8-9 = Data type: '0086' HEX for REAL matrices/'8086' HEX for COMPLEX matrices
% Bytes 10-15 = Reserved (zero padded)
%
[header , count] = fread(fid , 7 , 'uint16');
rows = header(1);
columns = header(2);
bytes = header(3);
data_type = header(4);
elseif fileFormat == hex2dec('EEDD') % Extended matrix file format.
%
% Read the matrix header information contained in the first 16-bytes.
%
% Bytes 0-1 = EEDD HEX identification flag (unsigned 2-byte integer)
% Bytes 2-3 = File type: '0086' HEX for REAL matrices/'8086' HEX for COMPLEX matrices (unsigned 2-byte integer)
% Bytes 4-7 = Number of rows (unsigned 4-byte integer)
% Bytes 8-11 = Number of columns (unsigned 4-byte integer)
% Bytes 12-15 = Size of the file minus this 16-byte header (unsigned 4-byte integer)
%
[data_type , count] = fread(fid , 1 , 'uint16');
[header , count] = fread(fid , 3 , 'uint32');
rows = header(1);
columns = header(2);
bytes = header(3);
end
%
% Read the data & resize the matrix if data consistency exists between the header & body.
%
if data_type == hex2dec('0086') % REAL Matrix
[data , count] = fread(fid , (rows * columns) , 'double');
if (rows * columns) == count
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. ')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -