Exporting Data from Matlab to BESA Statistics

From BESA® Wiki
Jump to: navigation, search

Introduction

This is a short tutorial on how to export MEG data from Matlab in a file format readable by BESA Statistics. For the purpose of this example, MEG data is used, however, the case of EEG data should not be very different. As an example the standard structure generated by FieldTrip after time-lock analysis is used (ft_timelockanalysis(...)) with the option

cfg.keeptrials = 'yes';
.

The structure looks like this:

timelock_cond1 = 
 
          avg: [147x475 double]
          var: [147x475 double]
         time: [1x475 double]
          dof: [147x475 double]
        label: {147x1 cell}
        trial: [100x147x475 double]
       dimord: 'rpt_chan_time'
         grad: [1x1 struct]
    trialinfo: [100x1 double]
          cfg: [1x1 struct]

The matrix 'avg' contains the averaged data for the current condition and the matrix 'trial' contains the single trial data. In this case there are 100 trials, the data contains 147 channels (this is a 148 channels BTI system but 1 channel was defined as bad) and 475 time samples. An important substructure is 'grad', it contains the positions of the MEG channels. In order to be able to follow the steps in this tutorial you need to download the scripts for exporting data from Matlab to BESA here: [1].

Exporting procedure

Let us assume that we already have the data for one subject and two conditions (cond1 and cond2) in these two structures: timelock_cond1 and timelock_cond2.

Exporting single trial data as ascii-vectorized files (.avr)

For the export of the single trial data in an .avr-file format we need the function besa_save2Avr(...) which is contained in the zip-file mentioned above.

Matlab code:

%% Export single trial data for condition 1
 
NumChannels = size(timelock_cond1.avg, 1);
NumTrials = size(timelock_cond1.trial, 1);
 
% the output directory
custom_path = 'D:\Data\C1';
% Multiply by 1000 to get the time in milliseconds.
time_samples = timelock_cond1.time.*1000;
channel_labels = timelock_cond1.label;
data_scale_factor = 1.0;
time_scale_factor = 1.0;
 
for iTr = 1:NumTrials
 
    % The file name where data should be written.
    file_name = ['Condition1Trial' num2str(iTr) '.avr'];
    % Multiply by 1e15 to get the data in femtoTesla.
    data_matrix = squeeze(timelock_cond1.trial(iTr, :, :)).*1e15;
 
    % Save the data
    besa_save2Avr(custom_path, file_name, data_matrix, time_samples, ...
        channel_labels, data_scale_factor, time_scale_factor);
 
end
 
%% Export single trial data for condition 2
 
NumTrials = size(timelock_cond2.trial, 1);
 
% the output directory
custom_path = 'D:\Data\C2';
% Multiply by 1000 to get the time in milliseconds.
time_samples = timelock_cond2.time.*1000;
channel_labels = timelock_cond2.label;
data_scale_factor = 1.0;
time_scale_factor = 1.0;
 
for iTr = 1:NumTrials
 
    % The file name where data should be written.
    file_name = ['Condition2Trial' num2str(iTr) '.avr'];
    % Multiply by 1e15 to get the data in femtoTesla.
    data_matrix = squeeze(timelock_cond2.trial(iTr, :, :)).*1e15;
 
    % Save the data
    besa_save2Avr(custom_path, file_name, data_matrix, time_samples, ...
        channel_labels, data_scale_factor, time_scale_factor);
 
end