Difference between revisions of "Exporting Data from Matlab to BESA Statistics"

From BESA® Wiki
Jump to: navigation, search
Line 82: Line 82:
 
end
 
end
 
</source>
 
</source>
 +
Please note that it is also possible to use the function <tt>besa_matrix2Gen(...)</tt> to save the data in a binary format instead of ASCII-format and thus obtain a better precision; however, you are going to be able to load this data only in BESA Statistics 2.0 and not in version 1.0.
 +
=== Rearrange the channel positions ===
 +
Sometimes it can happen that the order of the channels in the main structure (e.g. <tt>timelock_cond1.avg</tt>, <tt>timelock_cond1.label</tt>, <tt>timelock_cond1.trial</tt> etc) is different compared to the order of the channels in the <tt>grad</tt>-substructure (e.g. <tt>timelock_cond1.grad.chanpos</tt>, <tt>timelock_cond1.grad.label</tt> etc). In this case we have to rearrange the channel positions in the <tt>grad</tt>-substructure to have the same order as in the main structure.
 +
 +
'''Matlab code:'''

Revision as of 10:43, 8 April 2016

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

Please note that it is also possible to use the function besa_matrix2Gen(...) to save the data in a binary format instead of ASCII-format and thus obtain a better precision; however, you are going to be able to load this data only in BESA Statistics 2.0 and not in version 1.0.

Rearrange the channel positions

Sometimes it can happen that the order of the channels in the main structure (e.g. timelock_cond1.avg, timelock_cond1.label, timelock_cond1.trial etc) is different compared to the order of the channels in the grad-substructure (e.g. timelock_cond1.grad.chanpos, timelock_cond1.grad.label etc). In this case we have to rearrange the channel positions in the grad-substructure to have the same order as in the main structure.

Matlab code: