[讨论] C语言float64之dat档于matlab开启

楼主: xx013652 ( )   2017-04-18 13:57:58
有一dat档以二进制撰写,想以matlab开启,程式码如下:
fid = fopen(file,'rb');
Data= fread(fid,'float')
fclose(fid)
这个float应该等同float32
我的疑问在于,此dat档原本是以C语言撰写,它原本似乎是以float64记录资料
但为何我现在以float32开启会正确;以float64开启却错误?
错误的意思是数值不对,而非无法开启
同样的,有另一笔数据在C语言时以int32记录,但在matlab中却要用int16才能正确开启
想了解可能有哪些因素造成这个结果?
小弟对C语言的基础完全是0,上述有误恳请包涵,谢谢大家
以下是C语言之部分程式码,供参考:
har *test, *test1;
float nouse=123.456;
test=strstr(dataName,".txt"); // 搜寻档案名称是否为.txt档或.TXT档
(text file)
test1=strstr(dataName,".TXT");
dtime=1.0/frequency; ttime=0.0;
if (test==NULL && test1==NULL) { // 如果没有搜寻到,则表示该档案为二进
位档(Binary file)
ktext=1; // Binary file
fp1=fopen(dataName,"wb"); // 存资料的档案名称
fwrite(&nchannel, 2, 1, fp1); //No of channels
ifrequency=(int)frequency; //将freqency 从float64 转换成int 给
ifrequency 储存
fwrite(&ifrequency, 2, 1, fp1); //Sample rate
fwrite(&nouse, 4,1,fp1); //a float (nouse now)
}
else {
ktext=0; // Text file
fp1=fopen(dataName,"w"); // 存资料的档案名称
fprintf(fp1," %d \n",nchannel);
ifrequency=(int)frequency; //将freqency 从float64 转换成int 给
ifrequency 储存
fprintf(fp1," %d \n",ifrequency);
fprintf(fp1," %7.3f ",nouse);
}
//
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//
for (int32 k=0;k<300000000;k++){
if(starthour!=-1) {GetLocalTime(&st);} // 进入循环内再取得系统时间
if(starthour!=-1) {if(st.wHour>=endhour && st.wMinute >= endminute)
break;}
// 当现在时间超过结束时间跳出
循环不再量测
DAQmxErrChk
(DAQmxReadAnalogF64(taskHandle,nx,10.0,DAQmx_Val_GroupByChannel,data,nxg,&read,NULL));
ttt=cputime();
wsprintf(textoutscreen,"%d s",(int32)ttt); // 将量测的时间存入textoutscreen
阵列里
SetTextColor(hdc,RGB(0,0,255)); // 设定文字颜色(蓝)
TextOut(hdc,15.0,0.00*h,textoutscreen,(int)strlen(textoutscreen));
// 将量测时间显示在萤幕上
for (i=0; i<nxg; i++) xdata[i]=(float)data[i];
// 将data转换成float型态,丢给
xdata储存
datawrite(data,nx,ktext);
// 进入datawrite副程式写入资

showinformation(); // show information
作者: mp19990920 (郝渴连)   2017-04-18 17:08:00
float64?? float 是 4Byte 而 double 才是 8Byte 吧
楼主: xx013652 ( )   2017-04-18 17:37:00
float=float32 都是4bytes;float64是8bytes。help查的(help找fread,里面的范例提到的)
作者: sppmg (sppmg)   2017-04-18 21:12:00
float64==double,我没有看到c里面有提到,只有DAQ函式有,但未知传入型态是否正确(不确定传成float32是否会错误)另外,int,float长度未必是32b,确切要由sizeof测。简单一点,你用hex编辑器去检查档案,就能确定档案数值长度了!https://github.com/sppmg/matlab/tree/develop/daq这我以前做的MATLAB操作nidaq程式库。 详情见readme.html。daqmx_task是操作物件,会使用到midlib目录中的m2c转换程式。你参考一下,也欢迎直接拿去用XD

Links booklink

Contact Us: admin [ a t ] ucptt.com