-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractTsd.m
75 lines (68 loc) · 2.88 KB
/
extractTsd.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function extractTsd(folderData, target)
% the function should receive the folder ending by a /
%%%%%%%%%%%--- load the tsd file to read tsds array ---%%%%%%%%%%%
addpath('./tsdPackage/')
folderData = [folderData filesep];
%%%%%%%%%%%--- LOAD NEEDED RESOURCES ---%%%%%%%%%%%
Behavior=importdata(strcat(folderData,'behavResources.mat'));
disp('Data Loaded.')
disp(['target: ', target]);
if strcmp(target, 'pos')
try
X = Data(Behavior.("AlignedXtsd"));
Y = Data(Behavior.("AlignedYtsd"));
T = Range(Behavior.("AlignedXtsd"), 's');
catch
X = Data(Behavior.("Xtsd"));
Y = Data(Behavior.("Ytsd"));
T = Range(Behavior.("Xtsd"), 's');
end
V = Data(Behavior.("Vtsd"));
behavior.positions = [X Y];
behavior.position_time = T;
behavior.speed = V;
else
behavior.positions = Data(Behavior.(target));
behavior.position_time = Range(Behavior.(target), 's');
end
if isfield(Behavior,'SessionEpoch')
sessionNames = fieldnames(Behavior.SessionEpoch);
behavior.SessionNames = {};
behavior.SessionStart = [];
behavior.SessionStop = [];
for k = 1:length(sessionNames)
epochk = Behavior.SessionEpoch.(sessionNames{k});
behavior.SessionStart(k) = Start(epochk, 's');
behavior.SessionStop(k)= Stop(epochk, 's');
behavior.SessionNames(k) = {sessionNames{k}};
end
behavior.sleepPeriods = [];
else
behavior.SessionNames = {};
behavior.SessionNames(1) = {'Recording'};
behavior.SessionStart = [];
behavior.SessionStart(1) = behavior.position_time(1);
behavior.SessionStop = [];
behavior.SessionStop(1) =behavior.position_time(end);
behavior.sleepPeriods = [];
end
%% Get sleep periods if thy exist
if isfield(Behavior,'SessionEpoch')
sleep_pattern = arrayfun(@(x) strfind(lower(behavior.SessionNames{x}), 'sleep'), 1:length(behavior.SessionNames), ...
'UniformOutput', false); % Find it with no register
bool_sleep = arrayfun(@(x) ~isempty(sleep_pattern{x}), 1:length(sleep_pattern));
id_sleep = find(bool_sleep);
if sum(bool_sleep) > 0
behavior.sessionSleepNames = behavior.SessionNames(bool_sleep);
behavior.sleepPeriods = [behavior.SessionStart(id_sleep(1)) behavior.SessionStop(id_sleep(1))];
if sum(bool_sleep) > 1
for isleep = 2:length(id_sleep)
behavior.sleepPeriods = [behavior.sleepPeriods ...
behavior.SessionStart(id_sleep(isleep)) behavior.SessionStop(id_sleep(isleep))];
end
end
end
end
save(strcat(folderData,'nnBehavior.mat'),'behavior','-v7.3');
disp('Behavior is successfully extracted')
end