-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert_SessionData.m
55 lines (42 loc) · 2.2 KB
/
Convert_SessionData.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
% This code convert SessionData files generated by the behavior padadigm
% "LickToGetReward_Traing" and "LickToGetReward_5" into
% adjusted_SessionData files. In the adjusted files Cue start is adjusted
% to 0. This is for the processing in Python. Updated: 2023-04-18
% Get a list of all .mat files in the directory
files = dir('*.mat');
today_date = datestr(now, 'yyyy_mm_dd');
% Loop through all .mat files in the directory
for file_idx = 1:length(files)
file_name = files(file_idx).name;
% Load the original data
load(file_name);
% Create a copy of the original data
adjusted_SessionData = SessionData;
% Loop through all trials and adjust the timestamps
for i = 1:numel(SessionData.RawEvents.Trial)
% Get the cue start time for this trial
cue_start = SessionData.RawEvents.Trial{1,i}.States.Cue(1);
% Subtract cue start time from various event times
adjusted_SessionData = adjust_event_times(SessionData, adjusted_SessionData, i, cue_start);
end
% Save the adjusted data
adjusted_file_name = [file_name(1:end-4) '_' today_date '_Cue0_adjusted_data.mat'];
save(adjusted_file_name, 'adjusted_SessionData');
end
function adjusted_SessionData = adjust_event_times(SessionData, adjusted_SessionData, trial_idx, cue_start)
event_names = {'TimerTrig', 'TrialStart', 'ITI', 'Cue', 'CounterReset', 'WaitForLick', ...
'Reward', 'Drinking', 'UnconditionedReward'};
for j = 1:length(event_names)
event_name = event_names{j};
adjusted_SessionData.RawEvents.Trial{1,trial_idx}.States.(event_name) = ...
SessionData.RawEvents.Trial{1,trial_idx}.States.(event_name) - cue_start;
end
if isfield(SessionData.RawEvents.Trial{1,trial_idx}.Events, 'BNC1High')
adjusted_SessionData.RawEvents.Trial{1,trial_idx}.Events.BNC1High = ...
SessionData.RawEvents.Trial{1,trial_idx}.Events.BNC1High - cue_start;
end
if isfield(SessionData.RawEvents.Trial{1,trial_idx}.Events, 'BNC1Low')
adjusted_SessionData.RawEvents.Trial{1,trial_idx}.Events.BNC1Low = ...
SessionData.RawEvents.Trial{1,trial_idx}.Events.BNC1Low - cue_start;
end
end