How to read long double?

10 views (last 30 days)
Andrew
Andrew on 24 Dec 2012
Hello community,
I need to read long double (C++) from binary file in matlab. Unfortunetly, long double has 10 bytes order (80 bites). How can I do that? Any idea?
Thanks in advance.
Andrew

Answers (1)

José-Luis
José-Luis on 24 Dec 2012
Edited: José-Luis on 25 Dec 2012
I think the long double type is dependent on the architecture. If you wanted a really robust routine, you could read bit by bit and then transform your data, assuming you know the layout of long double.
doc fread
A short example:
fid = fopen('your_file.bin','r');
A = fread(fid, number_of_values * 2, 'ubit40'); %If your type is 80 bits long
fclose(fid);
Then you would need to reshape A, and find out the double values from the bits. All this might be pointless though, as I think Matlab does not support long doubles natively, so that precision might go to waste. You might be better off typecasting to double in your C++ code before importing to Matlab.
Then again, someone here might correct me.

Community Treasure Hunt

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

Start Hunting!