Read double precision numbers from binary file

조회 수: 3 (최근 30일)
Franck D'Araujo
Franck D'Araujo 2015년 3월 4일
댓글: Franck D'Araujo 2015년 3월 5일
Hello,
I have to read double precision numbers from a binary file. I'm used to do that in C, which basically looks like that for a LSB first coded file :
char *cAddress = NULL;
double value = 0;
cAddress = (char*)(value);
*cAddress = ucBuffer[uiOffset];
*(cAddress + 1) = ucBuffer[uiOffset + 1];
*(cAddress + 2) = ucBuffer[uiOffset + 2];
*(cAddress + 3) = ucBuffer[uiOffset + 3];
*(cAddress + 4) = ucBuffer[uiOffset + 4];
*(cAddress + 5) = ucBuffer[uiOffset + 5];
*(cAddress + 6) = ucBuffer[uiOffset + 6];
*(cAddress + 7) = ucBuffer[uiOffset + 7];
Now, I would like to implement that in Matlab. Do you have any idea on how I should proceed ?
Thanks a lot for supporting
Franck

채택된 답변

James Tursa
James Tursa 2015년 3월 4일
편집: James Tursa 2015년 3월 4일
Did you mean to type the following:
cAddress = (char*)(&value); // Added the & for type punning
Otherwise, the method of getting the bytes into the double isn't apparent. In any event, you should be able to simply use fopen with the proper machinefmt option, followed by fread with the '*double' option. Are doubles the only thing(s) in the file?
  댓글 수: 3
James Tursa
James Tursa 2015년 3월 4일
... the code most likely crashes."
I don't even think it will compile. I would be surprised if a compiler let you convert a double value directly into a pointer.
Franck D'Araujo
Franck D'Araujo 2015년 3월 5일
You are right, I forgot the &. That's just because the code is a copy/paste from a function where value is already a pointer !
I will take a close look to the fread documentation like proposed.
Thanks for helping
Franck

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2015년 3월 4일
편집: Adam 2015년 3월 4일
doc fread
That help page should give you the information you need on precision and other aspects of binary file reading, together with examples. It should be a lot simpler than the raw C equivalent.
  댓글 수: 1
Franck D'Araujo
Franck D'Araujo 2015년 3월 5일
Hello, I checked the fread doc, and it's just perfect for my needs. Thank you for pointing it out ! I'm more familiar with C programming than Matlab, so I didn't realize that the Matlab fread function had so many options, particularly the endianness management !
Thanks again for helping
Franck

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by