Creating SPM multiple conditions files from E-Prime edat-files using Matlab

EPrime to SPM multiple conditions file This page describes a small matlab script that can be used to convert E-Prime edat-files to SPM multiple conditions files. Collecting onset times and durations from experimental or behavioral data can be a tedious task and is error-prone when done manually or semi-automatically (e.g. using Excel).

SPM uses a very specific file format for defining experimental conditions. It requires a Matlab data file containging names and onset time and duration vectors for each condition. Also, onset times should be specified relative to the start of the first EPI scan, so, this might require additional post-processing of the onset times which are available in the E-Prime log files (unless this time shift correction was already performed in E-Prime using inline script).

Step 1 - Merge all edat files

This step is optional, but is easy to do and can save a lot of time when processing a large collection of edat-files of a single type of experiment. It will make the Matlab script (described in step 3 below) slightly more complicated, but there is no additional effort when using the example code. Here is what you do: Just use the E-Merge utility provided by PST to merge all individual edat-files into a single emrg-file. This new file has the same structure as the original edat-files, but contains the data of all included datasets. You can open and view the merged file as a regular edat-file (i.e. use E-DataAid to open the file). The most important thing to keep in mind is that the Subject and Session attributes of the merged file should be used at some point to differentiate between different sessions.

Step 2 - Export relevant columns using E-DataAid

This step is required to be able to load the E-Prime data into another program, such as Matlab. Start by opening the merged E-Prime file in E-DataAid. By default, all logged attributes are displayed as separate columns. Only a small subset is needed to create the onset times and durations vectors, so use the 'Arrange Columns' option from the Tools menu to make a selection. First move all attributes to the left. Then, select relevant attributes and add them back to the list on the right side. Typically you would like to include Subject and Session numbers. Then, add the attributes which specify the experimental conditions of the trials. Finally, add the attributes which contain the onset times and durations of the relevant events/epochs for all conditions. For FMRI studies you normally also require the onset time of the first EPI (i.e. start of the scanner). This is required to be able to calculate the relative onset times. Alternatively, this subtraction can be performed using E-Prime inline script and stores as separate attribute.

E-DataAid column selection

Accept your selection by clicking the OK button. E-DataAid will now only show (and export) the selected attributes. As you can see in the following figure, E-Prime includes missing values (NULL) in case an attribute-value was not available for the given trial. This can happen when other trial procedures are also played (e.g. baseline or instruction screens). Although the Matlab script can work around missing values, it is often more convenient to filter them out before exporting the data.

Reduced table

You can define a filter using the 'Filter' option from the Tools menu. In this example a filter was defined to exclude all trials of type 0.

filter definition

E-DataAid will now only show rows of TrialType 1 and 2 and exclude type 0, which are irrelevant for the fMRI analysis. Note that it is not always possible to remove rows containing missing values. In that case you can simply include missing values in the output and assign a unique value so the Matlab script can handle them properly (see below).

filtered rows

Ideally, all exported attributes contain numerical values only. This is not required, but the Matlab script below will be more complex if you include non-numerical attributes. As a workaround you could use Excel and find&replace all text with unique numerical values after exporting to the comma-separated values file. An even better approach is to think of this when creating the E-Prime script and fill such attributes using numerical values only.

When the selected rows and columns are OK, you can export the complete table as a comma-separated values file by selecting Export from the File menu. The following screen dump shows the preferred settings for loading the exported file into Matlab.

export csv-file

 

Step 3 - Prepare and run the Matlab script.

This step involves the creation of a short Matlab script that generates the onset time and duration vectors for all experimental conditions. Separate SPM compatible conditions files are created for all subjects. In this example we assume there is only one session for each subject. A nested for-loop is required when the dataset contains more than one sessions (see below).

The first thing to do is to start Matlab and open de script editor with a new blank M-file. (Or use the template script, which can be downloaded below.) Then, enter the following line to load the exported E-Prime data into a new variable T. (Use the same filename you used when exporting the data.) If the file is not placed in the current directory, you might want to type the complete path before the filename.

T = csvread('c:\mydata\exported_eprime_data.txt' ,0,0);

The next part of the script is not required, but makes the script a bit more readable. This simply defines recognisable variables for the columns.

SUBJECTNR = 1;
SESSIONNR = 2;
SCANNER_ONSET = 3;
TRIALTYPE = 4;
REWARD = 5;
CUE_ONSET = 6;
CUE_DURATION = 7;
TARGET_ONSET = 8;
TARGET_DURATION = 9;

The rest of the script is a simple loop over all subject numbers. The subject numbers are retrieved automatically from the first column using the matlab function unique. The first part of the loop is used to predefine 3 cells (onsets, durations and names) for 2 conditions, which will be exported later to the SPM multiple conditions file. The body of the loop also contains 2x5 lines of script to retrieve the onsets and durations of the two conditions of this example. The tricky part is the line where the find-function is used to select the right rows of table T. For both conditions a test is included to make sure that only rows of the 'current' subject are selected (T(:,SUBJECTNR)==subject). The other part of the test is to include reward or non-reward trials. Both tests are combines using the and-operator (&). The multiplication factor 0.001 is used to convert milliseconds to seconds. The sprintf function is used to create a unique filename for each subject and session.

subjects = unique(T(:,SUBJECTNR));

for iSubject=1:length(subjects)

subject = subjects(iSubject);
onsets = cell(1,2);
durations = cell(1,2);
names = cell(1,2);
condition_nr = 0;

% cue non-reward
condition_nr = condition_nr + 1;
names{condition_nr} =
'cue-non-reward';
I = find(T(:,REWARD)==0 & T(:,SUBJECTNR)==subject);
onsets{condition_nr} = 0.001 .* T(I,CUE_ONSET);
durations{condition_nr} = 0.001 .* T(I,CUE_DURATION);

% cue reward
condition_nr = condition_nr + 1;
names{condition_nr} =
'cue-reward';
I = find(T(:,REWARD)==1 & T(:,SUBJECTNR)==subject);
onsets{condition_nr} = 0.001 .* T(I,CUE_ONSET);
durations{condition_nr} = 0.001 .* T(I,CUE_DURATION);

save(sprintf('conditions_%06d.mat',subject) , 'names', 'onsets', 'durations');

end

That's it! Just modify the above script to make it suitable for your own E-Prime output, and you are ready to go. Simply run the script an you will get multiple conditions files in less than a second...

Multiple sessions

Handling multiple sessions per subject is just a matter of extending the script:

subjects = unique(T(:,SUBJECTNR));
sessions = unique(T(:,SESSIONNR));

for iSubject=1:length(subjects)

for iSession=1:length(sessions)

...

I = find(T(:,REWARD)==0 & T(:,SUBJECTNR)==subject & T(:,SESSIONNR)==session);

save(sprintf('conditions_%06d_%02.mat',subject,session) , 'names', 'onsets', 'durations');

 

end

 

end

 

Download

Matlab template script: spm_mult_cond_template.m

 

Free Software Disclaimer

The free software programs provided by 3TMRI.nl may be freely distributed, provided that no charge above the cost of distribution is levied, and that the disclaimer below is always attached to it.
The programs are provided as is without any guarantees or warranty. Although the author has attempted to find and correct any bugs in the free software programs, the author is not responsible for any damage or losses of any kind caused by the use or misuse of the programs.
The author is under no obligation to provide support, service, corrections, or upgrades to the free software programs. For more information, please send and email to the  This email address is being protected from spambots. You need JavaScript enabled to view it. support mailbox.