Skip to content

Export Continuous Data To Binary File

Import continuous data into MATLAB using TDTbin2mat
Export the data to a binary file (f32 floating point or 16-bit integer)
Channels are interlaced in the final output file
Good for exporting to other data analysis applications

Download M File

Housekeeping

Clear workspace and close existing figures. Add SDK directories to MATLAB path.

close all; clear all; clc;
[MAINEXAMPLEPATH,name,ext] = fileparts(cd); % \TDTMatlabSDK\Examples
DATAPATH = fullfile(MAINEXAMPLEPATH, 'ExampleData'); % \TDTMatlabSDK\Examples\ExampleData
[SDKPATH,name,ext] = fileparts(MAINEXAMPLEPATH); % \TDTMatlabSDK
addpath(genpath(SDKPATH));

Importing the Data

This example assumes you downloaded our example data sets and extracted it into the \TDTMatlabSDK\Examples directory. To import your own data, replace 'BLOCKPATH' with the path to your own data block.

In Synapse, you can find the block path in the database. Go to Menu → History. Find your block, then Right-Click → Copy path to clipboard.

BLOCKPATH = fullfile(DATAPATH,'Algernon-180308-130351');

Setup the variables for the data you want to extract

We will extract the stream stores and output them to 16-bit integer files

FORMAT = 'i16'; % i16 = 16-bit integer, f32 = 32-bit floating point
SCALE_FACTOR = 1e6; % scale factor for 16-bit integer conversion, so units are uV
% Note: The recommended scale factor for f32 is 1

read the first second of data to get the channel count

data = TDTbin2mat(BLOCKPATH, 'TYPE', {'streams'}, 'T2', 1);
read from t=0.00s to t=1.00s

If you want an individual store, use the 'STORE' filter like this:

data = TDTbin2mat(BLOCKPATH, 'TYPE', {'streams'}, 'STORE', 'Wav1', 'T2', 1);

Loop through all the streams and save them to disk in 10 second chunks

fff = fields(data.streams);
TIME_DELTA = 10;
for ii = 1:numel(fff)
    T1 = 0;
    T2 = T1 + TIME_DELTA;

    thisStore = fff{ii};
    OUTFILE = fullfile(BLOCKPATH, [thisStore '.' FORMAT]);

    fid = fopen(OUTFILE, 'wb');

    data = TDTbin2mat(BLOCKPATH, 'STORE', fff{ii}, 'T1', T1, 'T2', T2);

    % loop through data in 10 second increments
    while ~isempty(data.streams.(thisStore).data)
        if strcmpi(FORMAT, 'i16')
            fwrite(fid, SCALE_FACTOR*reshape(data.streams.(thisStore).data, 1, []), 'integer*2');
        elseif strcmpi(FORMAT, 'f32')
            fwrite(fid, SCALE_FACTOR*reshape(data.streams.(thisStore).data, 1, []), 'single');
        else
            warning('Format %s not recognized. Use i16 or f32', FORMAT);
            break
        end
        T1 = T2;
        T2 = T2 + TIME_DELTA;
        data = TDTbin2mat(BLOCKPATH, 'STORE', fff{ii}, 'T1', T1, 'T2', T2);
    end
    fprintf('Wrote %s to output file %s\n', thisStore, OUTFILE);
    fprintf('Sampling Rate: %.6f Hz\n', data.streams.(thisStore).fs);
    fprintf('Num Channels: %d\n', size(data.streams.(thisStore).data, 1));
    fclose(fid);
end
read from t=0.00s to t=10.00s
read from t=10.00s to t=20.00s
read from t=20.00s to t=30.00s
read from t=30.00s to t=40.00s
read from t=40.00s to t=50.00s
read from t=50.00s to t=60.00s
read from t=60.00s to t=70.00s
read from t=70.00s to t=80.00s
Wrote pNe1 to output file C:\TDT\TDTMatlabSDK\Examples\ExampleData\Algernon-180308-130351\pNe1.i16
Sampling Rate: 498.246185 Hz
Num Channels: 16
read from t=0.00s to t=10.00s
read from t=10.00s to t=20.00s
read from t=20.00s to t=30.00s
read from t=30.00s to t=40.00s
read from t=40.00s to t=50.00s
read from t=50.00s to t=60.00s
read from t=60.00s to t=70.00s
read from t=70.00s to t=80.00s
Wrote Wav1 to output file C:\TDT\TDTMatlabSDK\Examples\ExampleData\Algernon-180308-130351\Wav1.i16
Sampling Rate: 24414.062500 Hz
Num Channels: 16
read from t=0.00s to t=10.00s
read from t=10.00s to t=20.00s
read from t=20.00s to t=30.00s
read from t=30.00s to t=40.00s
read from t=40.00s to t=50.00s
read from t=50.00s to t=60.00s
read from t=60.00s to t=70.00s
read from t=70.00s to t=80.00s
Wrote LFP1 to output file C:\TDT\TDTMatlabSDK\Examples\ExampleData\Algernon-180308-130351\LFP1.i16
Sampling Rate: 3051.757813 Hz
Num Channels: 16