在MATLAB中读取和写入二进制文件的字符串和整数

时间:2015-04-22 21:34:21

标签: matlab binary

我正在尝试编写,然后将以下数据读取到二进制文件中,但输出错误。

str_cetag = 'AIMIDB';
cel_num = 00089;
acq_date = 20150422110459;

% Open and write to file
fid = fopen('BFile.bin','w');
fwrite(fid,str_cetag);
fwrite(fid,cel_num);
fwrite(fid,acq_date);
fclose(fid);

% Open and read from file
fid = fopen('BFile.bin','r');
fread(fid,6,'uint8=>char')';
fread(fid,5,'uint8=>double')';
fread(fid,14,'uint8=>double')';
fclose(fid);

% Output
ans = 0
ans = AIMIDB
ans = 89   255   127
ans = []
ans = 0

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:0)

查看输出后,您可能会注意到str_cetagcel_num已正确写入和读取。问题是acq_date太大而无法存储为8位无符号整数,这是fwrite的默认值。

这是正常的:

str_cetag = 'AIMIDB';
cel_num = 89;
acq_date = 20150422110459;

% Open and write to file
fid = fopen('BFile.bin','w');
fwrite(fid,str_cetag);
fwrite(fid,cel_num,'uint8');
fwrite(fid,acq_date,'uint64');
fclose(fid);

% Open and read from file
fid = fopen('BFile.bin','r');
fread(fid,6,'uint8=>char')'
fread(fid,1,'uint8')'
fread(fid,'uint64')'
fclose(fid);