[討論] 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