Search code examples
matlabbinarylabview

Retrieve data from .rec binary file


The question may be naive, but answers could help me.

A measurement is recorded in binary format, with a header that contains all information about the data and the data itself (i.e. a series of doubles).

The measurement data can be exported in csv format from the application, but it takes ages.

What do you have to pay attention to when trying to read data from a binary file? Is this process even feasible using Matlab to import as an array or labview (export as .txt maybe?)


Solution

  • Binary .rec file format may refer to various things (audio/video encoding format of Topfield based on MPEG4-TS, proprietary audio encoding, and even MRI scanner from Phillips) ...

    If it refers to MRI scanner you may find some direct reader on fileexchange: Matlab PAR REC Reader

    If it refer to something else, you may parse binary file header and data by yourself using the low level routine: fread

    Edit

    Not knowing the exact file format for your recorded sensor displacement, here is dummy example with fread for reading large rec file block-by-block supposing header contains just the length of data, and that data is just a serie of double values:

    function [] = DummyReadRec()
    %[
        % Open rec file for reading
        [fid, errmsg] = fopen('dummy.rec', 'r');
        if (fid < 0), error(errmsg); end
        cuo = onCleanup(@()fclose(fid));
    
        % Read header (here supposing it is only an integer giving length of data)
        reclenght = fread(fid, 1, 'uint32');
    
        % Read data block-by-block (here supposing it is only double values)
        MAX_BLOCK_LENGTH = 512;
        blockCount = ceil(reclenght / MAX_BLOCK_LENGTH);
        for bi = 1:blockCount,
    
            % Will read a maximum of 'MAX_BLOCK_LENGTH' (or less if we're on the last block) 
            [recdata, siz] = fread(fid, [1 MAX_BLOCK_LENGTH], 'double');
    
            % Do something with this block (fft or whatever)
            offset = (bi-1)*MAX_BLOCK_LENGTH;
            position = (offset+1):(offset+siz);
            plot(position, 20*log10(abs(fft(recdata))));
            drawnow();
    
        end
    %]
    end