MATLAB读写txt文件,并对数据进行二/十/十六进制转换(量化)

clear;
close all;
clc;

% 读取数据txt(任意进制、整型、浮点型)
fileID1 = fopen('D:\设计文档\dataset1_I.txt','r');
[dat_i]=textscan(fileID1,'%f');
fclose(fileID1);

fileID2 = fopen('D:\设计文档\dataset1_Q.txt','r');
[dat_q]=textscan(fileID2,'%f');
fclose(fileID2);

% cell转化为数组
dat_i_h = cell2mat(dat_i);
dat_q_h = cell2mat(dat_q);

% 量化为十进制有符号数(含符号位共14bit)
m = max(max(abs(dat_i_h),abs(dat_q_h)));
DATA_I = round(dat_i_h/m*(2^(14-1)-1));
DATA_Q = round(dat_q_h/m*(2^(14-1)-1));

% 将十进制数据写入txt
fileID_I = fopen('D:\intel_pro\RadarECG\rtl\dataset1_I_dec.txt','w');
if fileID_I == -1
  disp('fopen failed');
else
  for i=1:length(DATA_I)
    fprintf(fileID_I,'%d\n',DATA_I(i));
  end
end
fclose(fileID_I);

fileID_Q = fopen('D:\intel_pro\RadarECG\rtl\dataset1_Q_dec.txt','w');
if fileID_Q == -1
  disp('fopen failed');
else
  for j=1:length(DATA_Q)
    fprintf(fileID_Q,'%d\n',DATA_Q(j));
  end
end
fclose(fileID_Q);

% % 量化为十六进制有符号数(含符号位共14bit)
% m = max(max(abs(dat_i_h),abs(dat_q_h)));
% DATA_I = dec2hex(round(dat_i_h/m*(2^(14-1)-1)));
% DATA_Q = dec2hex(round(dat_q_h/m*(2^(14-1)-1)));
%
% % 将十六进制数据写入txt
% fileID_I = fopen('D:\intel_pro\RadarECG\rtl\dataset1_I_hex.txt','w');
% if fileID_I == -1
%    disp('fopen failed');
% else
%    for i=1:length(DATA_I)
%       fprintf(fileID_I,'%c%c%c%c\n',DATA_I(i,1),DATA_I(i,2),DATA_I(i,3),DATA_I(i,4));
%    end
% end
% fclose(fileID_I);
%
% fileID_Q = fopen('D:\intel_pro\RadarECG\rtl\dataset1_Q_hex.txt','w');
% if fileID_Q == -1
%    disp('fopen failed');
% else
%    for j=1:length(DATA_Q)
%      fprintf(fileID_Q,'%c%c%c%c\n',DATA_Q(j,1),DATA_Q(j,2),DATA_Q(j,3),DATA_Q(j,4));
%    end
% end
% fclose(fileID_Q);

 

posted @ 2021-08-04 11:40  最浪三叔  阅读(3233)  评论(0编辑  收藏  举报