Read and convert a .dat file containing binary data

31 views (last 30 days)
Hi all,
I'm a beginner with the binary files and I would need some help. I have a *.dat file coming out from my CAEN digitizer that contains binary data. The structure of the file is the following:
EVENT=│Event│Event│Event│....│Event│; Event=(unsigned 64 bits int)Time Tag│(unsigned 16 bit)Energy
So the idea is: a 1-column (or 1-row, I'm not pretty sure) file containing data of timing and energy of alpha particles.
- First Event: timing of the first particle in unsigned 64 bits int.
- Second Event: Energy of the first alpha particle in unsigned 16 bit.
- Third Event: timing of the second particle in unsigned 64 bits int.
- Fourth Event: Energy of the second alpha particle in unsigned 16 bit.
And so on....I would like to convert the file into a 2-columns file containing:
- First column: timings
- Second column: energies
First in binary to really see the zeros and ones and then to ASCII. I attach one of those files (I had to change the extension to .txt to upload).
Any help will be very welcome. Jose Manuel.

Accepted Answer

dpb
dpb on 2 Jul 2014
Edited: dpb on 3 Jul 2014
EVENT=EventEventEvent....│Event│;
Event=(unsigned 64 bits int)Time Tag(unsigned 16 bit)Energy
In Matlab to read a file structure of differing sizes, you either have to loop over the file reading the proper size at each pass or use the memmapfile object to create the field structure.
A)
fid=fopen('yourfile');
te=[];
while ~feof(fid)
te=[te; [fread(fid,1,'uint64') fread(fid,1,'uint16')]];
end
fid=fclose(fid);
Now you've got a Nx2 double array. To see the ASCII rep for first 10 values--
>> num2str(te(1:10,:),'%d')
ans =
6892248 9310
17040576 9311
27189064 9309
37337248 9309
47485440 9318
57633536 9313
67781768 9318
77930056 9311
88078688 9313
98227224 9312
>>
or the hex or binary
>> [dec2bin(te(1:10,1)) ones(10,1)*' ' dec2bin(te(1:10,2))]
ans =
000011010010010101011011000 10010001011110
001000001000000010011000000 10010001011111
001100111101101111101001000 10010001011101
010001110011011100010100000 10010001011101
010110101001001001000000000 10010001100110
011011011110110101100000000 10010001100001
100000010100100010010001000 10010001100110
100101001010001111001001000 10010001011111
101001111111111100101100000 10010001100001
101110110101101010000011000 10010001100000
>>
B)
m = memmapfile('records.dat', ...
'Format', { ...
'uint64' [1 1] 't'; ...
'uint16' [1 1] 'e'});
The data is then accessed from the structure array m with the fields as above defined by m.data.t and m.data.e, respectively.
See the doc for both for more of the gory details...
  6 Comments
Jose Manuel Gomez Guzman
Jose Manuel Gomez Guzman on 4 Jul 2014
Dear dpb,
my fault. I used one of those online converters but I made a mistake by assuming that 98227224 was ASCII and not decimal. Now it has sense. Thanks. Can you suggest any freeware dump utility to open the files?.
dpb
dpb on 4 Jul 2014
OK, I knew something had to be misunderstanding; couldn't figure out just what... :)
I don't have any freeware suggestions for a dump utility; I'm sure there must be any number around. I use the command-line shell products from JPSoft which includes a very nice one as a feature; consequently am not up on what else is around. On Windows unless it's been removed in versions since XP (yes, I'm still in the dark ages here :) ), there is DEBUG that can be used as a rudimentary dump utility, but it's not much of a tool for the purpose.

Sign in to comment.

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!