【发布时间】:2012-01-19 11:30:29
【问题描述】:
我正在用 C++ 编写一个函数来加载基于 MATLAB® 的 MAT 文件(第 5 级)格式 MAT-File Format 2011b doc(参见 www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf)。
我必须遗漏一些东西(可能是 C++),因为字节数字段为零。 MAT-File 标头已成功读取,数据类型元素标志也是如此,但字节数未成功。加载mat文件的代码如下:
// file handler
ifstream file;
// open file
file.open(i_file, ifstream::in | ifstream::binary);
// check for errors
if (!file) return NULL;
/********** BEGIN MAT-File Header **********/
char header_text[116], header_offset[8], header_version[2], header_endian[2];
// The first 116 bytes of the header can contain text data in human-readable form.
file.read( (char*) &header_text, 116); cout << header_text << endl;
/* Header Subsystem Data Offset Field */
// Bytes 117 through 124 of the header contain an offset to subsystem-specific
// data in the MAT-file.
file.read( (char*) &header_offset, 8); cout << header_offset << endl;
/* Header Flag Fields */
// Version When creating a MAT-file, set this field to 0x0100.
file.read( (char*) &header_version, 2); cout << header_version << endl;
// Endian Indicator. Contains the two characters, M and I, written to the
// MAT-file in this order, as a 16-bit value.
file.read( (char*) &header_endian, 2); cout << header_endian << endl;
/********** END MAT-File Header **********/
/********** BEGIN MAT-File Data Element **********/
/* The Tag Field */
// The 8-byte data element tag is composed of two, 32-bit fields
// Data Type
__int32_t data_type = file.get(); cout << data_type << endl;
// Number of Bytes
__int32_t num_bytes = file.get(); cout << num_bytes << endl;
输出如下:
MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Fri May 20 18:21:46 2011
IM
15
0
我从 MATLAB 获得信息:
谁 - 文件 PaviaU.mat
名称大小字节类属性
paviaU 610x340x103 170897600 双
我是否以某种方式错误地从标头加载数据? 为什么字节数为零?
编辑:如果我按如下方式阅读数据元素(在一条评论中建议):
char data_type[4], num_bytes[4];
file.read((char*) &data_type, 4); cout << data_type << endl;
file.read((char*) &num_bytes, 4); cout << num_bytes << endl;
我在cout(二进制代码)上得到了意外的值
但调试函数我可以检查两个变量:
data_type[0] = 15
data_type[1] = 0 '\0'
data_type[2] = 0 '\0'
data_type[3] = 0 '\0'
num_bytes[0] = -3/253
num_bytes[1] = 27
num_bytes[2] = 19
num_bytes[3] = 2
data_type 的值为 15,但 num_bytes 中的 -3/253 呢?那是哪个号码?
【问题讨论】:
-
代码示例中的注释说标签由两个 32 位字段组成。但是您只读取 2 个字节(通常为 16 位)。
get()只读取一个char。 -
改用
file.read((char*) &data_type, 4);和file.read((char*) &num_bytes, 4)。 -
@jrok 谢谢你的建议。我已经用新的输出更新了问题。
-
我猜
num_bytes和data_type应该是无符号值。 -3/253 是以签名/未签名形式从文件中读取的值(char显然已在您的平台上签名)。将num_bytes和data_type的声明更改为uint32_t类型,应该没问题。