Skip to content

Commit 7a904d3

Browse files
committed
add save stim option
1 parent 1bdc47f commit 7a904d3

File tree

4 files changed

+88
-116
lines changed

4 files changed

+88
-116
lines changed

saveEventsFile.m

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
%
44
% INPUTS
55
%
6+
% logFile:
7+
% When you want to save your data logFile contains the data you want to save.
8+
% The logFile variable that contains the n events you want to % save must be a nx1 structure.
9+
% Each field will be saved in a separate column.
10+
%
11+
% example:
12+
% logFile(1,1).onset = 2;
13+
% logFile(1,1).trial_type = 'motion_up';
14+
% logFile(1,1).duration = 1;
15+
% logFile(1,1).speed = 2;
16+
% logFile(1,1).is_fixation = true;
17+
%
18+
% logFile(2,1).onset = 3;
19+
% logFile(2,1).trial_type = 'static';
20+
% logFile(2,1).duration = 4;
21+
% logFile(2,1).is_fixation = 3;
22+
%
23+
%
624
% action:
725
% - 'open': will create the file ID and return it in logFile.eventLogFile using the information in
826
% the expParameters structure. This file ID is then reused when calling that function to save data
@@ -28,81 +46,110 @@
2846
end
2947

3048
switch action
31-
49+
3250
case 'open'
33-
51+
3452
logFile = struct();
35-
53+
3654
% Initialize txt logfiles and empty fields for the standard BIDS
3755
% event file
3856
logFile.eventLogFile = fopen(...
39-
fullfile(expParameters.outputDir, expParameters.modality, expParameters.fileName.events), ...
57+
fullfile(...
58+
expParameters.outputDir, ...
59+
expParameters.modality, ...
60+
expParameters.fileName.events), ...
4061
'w');
41-
42-
% print the basic BIDS columns
43-
fprintf(logFile.eventLogFile, '%s\t%s\t%s\t', 'onset', 'trial_type', 'duration');
44-
45-
% print any extra column specified by the user
46-
% also prepare an empty field in the structure to collect data
47-
% for those
48-
for iExtraColumn = 1:numel(varargin)
49-
fprintf(logFile.eventLogFile,'%s\t', lower(varargin{iExtraColumn}));
50-
end
51-
52-
% next line so we start printing at the right place
53-
fprintf(logFile.eventLogFile, '\n');
54-
55-
62+
63+
initializeHeader(logFile, varargin);
64+
65+
66+
case 'open_stim'
67+
logFile = struct();
68+
69+
% Initialize txt logfiles and empty fields for the standard BIDS
70+
% event file
71+
logFile.eventLogFile = fopen(...
72+
fullfile(...
73+
expParameters.outputDir, ...
74+
expParameters.modality, ...
75+
expParameters.fileName.stim), ...
76+
'w');
77+
78+
initializeHeader(logFile, varargin);
79+
5680
case 'save'
57-
81+
82+
if ~isstruct(logFile) || size(logFile, 2)>1
83+
error('The logFile variable that contains the n events you want to save must be a nx1 structure.')
84+
end
85+
5886
% appends to the logfile all the data stored in the structure
5987
% first with the standard BIDS data and then any extra things
6088
for iEvent = 1:size(logFile,1)
61-
89+
6290
fprintf(logFile(1).eventLogFile,'%f\t%s\t%f\t',...
6391
logFile(iEvent).onset, ...
6492
logFile(iEvent).trial_type, ...
6593
logFile(iEvent).duration);
66-
94+
6795
for iExtraColumn = 1:numel(varargin)
68-
96+
6997
% if the field we are looking for does not exist or is empty in the
7098
% action logFile structure we will write a NaN otherwise we
7199
% write its content
72-
100+
73101
if ~isfield(logFile, varargin{iExtraColumn})
74102
data = [];
75103
else
76104
data = getfield(logFile(iEvent), varargin{iExtraColumn});
77105
end
78-
106+
79107
if isempty(data)
80108
data = NaN;
81109
end
82-
110+
83111
if ischar(data)
84112
fprintf(logFile(1).eventLogFile, '%s\t', data);
85113
else
86114
fprintf(logFile(1).eventLogFile, '%f\t', data);
87115
end
88-
116+
89117
end
90-
118+
91119
fprintf(logFile(1).eventLogFile, '\n');
92120
end
93-
121+
94122
case 'close'
95-
123+
96124
% close txt log file
97125
fclose(logFile(1).eventLogFile);
98-
126+
99127
if expParameters.verbose
100128
fprintf(1,'\nData were saved in this file:\n\n%s\n\n', ...
101129
fullfile(...
102130
expParameters.outputDir, ...
103131
expParameters.modality, ...
104132
expParameters.fileName.events));
105-
133+
106134
end
135+
136+
end
107137

108138
end
139+
140+
function initializeHeader(logFile, varargin)
141+
142+
% print the basic BIDS columns
143+
fprintf(logFile.eventLogFile, '%s\t%s\t%s\t', 'onset', 'trial_type', 'duration');
144+
145+
% print any extra column specified by the user
146+
% also prepare an empty field in the structure to collect data
147+
% for those
148+
for iExtraColumn = 1:numel(varargin{1})
149+
fprintf(logFile.eventLogFile,'%s\t', lower(varargin{1}{iExtraColumn}));
150+
end
151+
152+
% next line so we start printing at the right place
153+
fprintf(logFile.eventLogFile, '\n');
154+
155+
end

saveEventsFile.m~

Lines changed: 0 additions & 79 deletions
This file was deleted.

tests/test_createFilename.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function test_createFilename()
22

33
% test for filename creation and their directories
44

5-
%% check directory and filename creation (PC and eyetracker)
5+
%% check directory and filename creation (PC)
66

77
expParameters.subjectGrp = '';
88
expParameters.subjectNb = 1;
@@ -34,9 +34,11 @@ function test_createFilename()
3434
assert(strcmp(...
3535
expParameters.fileName.events, ...
3636
['sub-001_ses-001_task-testtask_run-001_events_date-' expParameters.date '.tsv']));
37-
37+
assert(strcmp(...
38+
expParameters.fileName.stim, ...
39+
['sub-001_ses-001_task-testtask_run-001_stim_date-' expParameters.date '.tsv']));
3840

39-
%% check directory and filename creation (fMRI)
41+
%% check directory and filename creation (fMRI and eye tracker)
4042

4143
clear
4244

tests/test_saveEventsFile.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ function test_saveEventsFile()
1818

1919

2020

21-
%% create the file
21+
%% create the events and stim files
2222

2323
logFile = saveEventsFile('open', expParameters, [], 'Speed', 'is_Fixation');
2424

25+
stimFile = saveEventsFile('open_stim', expParameters, []);
2526

2627
% ---- test section
2728

2829
fileName = fullfile(expParameters.outputDir, expParameters.modality, expParameters.fileName.events);
30+
stimFileName = fullfile(expParameters.outputDir, expParameters.modality, expParameters.fileName.stim);
2931

3032
% check that the file has the right path and name
3133
assert(exist(fileName, 'file')==2)
32-
34+
assert(exist(stimFileName, 'file')==2)
3335

3436

3537
%% write things in it

0 commit comments

Comments
 (0)