matlab如何讀入混合變數格式的dat文件?
01-05
現有一個DAT文件,DAT中的變數格式為:
unsigned short usDayCount; unsigned int uiMsCount; unsigned int uiMsAttCount; short sAttRoll;short sAttPitch;
short sAttYaw; unsigned int uiMsGpsCount; int X; int Y; int Z;變數是按聲明順序寫入的。請問:如何用matlab正確讀入DAT文件中的數據。
謝謝 @Falccm 的耐心指導。代碼寫完回來自己答一下這個問題,為以後有類似問題的知友提供參考。這個dat文件如falccm所說fread和 memmapfile都是可以讀取的。但由於我的處理系統和這個dat文件的生成系統的高低位不同,所以一開始怎麼讀取都不正確,之後注意下高低位問題就好了。這裡給一下正常情況讀取的代碼示例:
第一種:fread
fid = fopen("A.DAT","rb");for i=1:1000[b1(i),c1] = fread(fid,1,"uint16");[b2(i),c2] = fread(fid,1,"uint32");[b3(i),c3] = fread(fid,1,"uint32");[b4(i),c4] = fread(fid,1,"int16");[b5(i),c5] = fread(fid,1,"int16");[b6(i),c6] = fread(fid,1,"int16");[b7(i),c7] = fread(fid,1,"uint32");[b8(i),c8] = fread(fid,1,"int32");
[b9(i),c9] = fread(fid,1,"int32");[b10(i),c10] = fread(fid,1,"int32");endj=[b1;b2;b3;b4;b5;b6;b7;b8;b9;b10]";fclose(fid);第二種:memmapfilem = memmapfile("A.DAT",..."Format",{"uint16",[1,1],"Q;..."uint32",[1,1],"A";..."uint32",[1,1],"S";...
"int16",[1,1],"D";..."int16",[1,1],"F";..."int16",[1,1],"G";..."uint32",[1,1],"H";..."int32",[1,1],"X";..."int32",[1,1],"Y";..."int32",[1,1],"Z"},"Repeat",inf)A=m.data;以上。fread 可以,不過最合適的應該是建立文件的內存映射:Create memory map to a fileMap File to Memory
推薦閱讀:
※如何實現多維數組的行 / 列按照 index 訪問都得到連續內存?
※matlab有類似c語言struct又能用tabulate處理的類型嗎?
※matlab中,一個m文件為什麼設計成只能定義一個函數?
※電動汽車基於模型設計的實現思路是怎樣的?
※有哪些 Matlab 代碼分享網站?
TAG:MATLAB |