Converting binary 8 to decimal

Hi,
I am looking to convert a binary-8 data file to text. Please see the two attached files: the raw data is collected in binary 8 format, but for post-processing, I want to use the ASCII-deliminated format.
From the manual, which describes the format of the two files ( http://math.nist.gov/oommf/doc/userguide12a3/userguide/OVF_1.0_format.html ), the binary data is in 8-byte mode. I am trying to write a script which will convert the binary format to the matrix text format (I can convert the two manually, but want to automate the process).
I have tried using the fread function, but with no luck.
Thank you in advance!
Carl

 Risposta accettata

Walter Roberson
Walter Roberson il 28 Ott 2013

0 voti

"network byte order" is "big-endian", so fread() the data block with '*double', 'b' as options

7 Commenti

Carl
Carl il 28 Ott 2013
Modificato: Carl il 28 Ott 2013
% Hi Walter,
% Thanks for replying. I have tried this, but the output is not correct numerically?
% Here is the code I am using:
path = 'binary-8_data.txt';
A = fopen(path);
fread(A,'*double','b')
% This generates the following output:
ans =
1.0e+014 *
1.2346
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
% which does not match up with the text-formatted data.
% Can you see what is going wrong here?
% Thank you once again,
% Carl
You could try with 'l' instead of 'b'
Also remember that the binary-8 only applies to data blocks in that specification, whereas you seem to be reading from the beginning of the file (including the text line that introduces the data block.) You need to position the file to the beginning of the binary data block before doing the fread()
Carl
Carl il 28 Ott 2013
Sorry, I should have been more clear with the coding in my last comment. The binary file consists solely of binary data. I have deleted the first 8 characters from the binary file (as these are merely dummy characters, according to the webpage I linked to earlier), and the output is now a column vector with 12 rows. A good sign, I think, as the text file has 12 numerical elements.
The data values are however still far off from being right. I have noticed that the binary values are always, when converted to text, integers, whereas I expect decimal values. Is this something we need to address?
Thanks. Note I have edited my last comment, to make it clearer.
Try this:
path = 'binary-8_data.txt';
A = fopen(path);
B = fread(A,24,'*uint8','b');
fclose(A);
fprintf('%02X ', B);
fprintf('\n')
C = typecast(B(1:8),'double');
fprintf('%.99g\n', C);
and then if you could show us the output lines.
Also, if you could take the expected data for the first three values, and apply num2hex() to those values, and show us the result.
Carl
Carl il 29 Ott 2013
Modificato: Carl il 29 Ott 2013
% The output using your code is:
41 26 3F F6 E2 5C 1D 98 41 12 EA 6C 6A 48 D4 41 40 32 79 2A 9E 25 A6 9C -1.608943580019507e-192
% Applying num2hex to my first three expected output values gives:
412681f6e25c1d98 4112ea6c6a48d441 4032792a9e25a69c
Notice that if you skip the first 8 hex groups in the hex printout, that what remains is an exact match for the first two of your expected values.
So, your earlier code should work:
path = 'binary-8_data.txt';
A = fopen(path);
T = fread(A,'*double','b');
fclose(A);
except that you might need to discard the first value in T (possibly.) But to make it more clear to you that you have the data you want, before displaying T, give the command
format long g
and then display it. With the 12 values in hand, and 12 expected, you might be able to figure out why the first value in the file is not the one you are presently expecting.
Carl
Carl il 29 Ott 2013
Thank you very much! The error was arising from an error within the file: I tried this on a new similar file, and the output came out correct.
Once again thanks a lot.
Carl

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Richiesto:

il 28 Ott 2013

Commentato:

il 29 Ott 2013

Community Treasure Hunt

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

Start Hunting!

Translated by