-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMaskedData.m
52 lines (51 loc) · 1.9 KB
/
getMaskedData.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
function [alignedData, median_t] = getMaskedData(data, dataMask, trialNum, nanReversePadding)
n_signals = size(data,3);
%nanReversePadding- if true the first columns are filled with nan's rather
%than the typical nan padding at the end
if nargin<4
nanReversePadding = false;
end
if nargin<3 || isempty(trialNum)
if max(dataMask(:)) == 1 %If logicals or 0/1 numbers
n_tr = size(dataMask,1);
n_samps = sum(logical(dataMask),2);
max_t = max(n_samps);
alignedData = nan(n_tr, max_t, n_signals);
for tr = 1:n_tr
if ~nanReversePadding
alignedData(tr,1:n_samps(tr),:) = data(tr, logical(dataMask(tr,:)), :);
else
alignedData(tr,(end-n_samps(tr)+1):end,:) = data(tr, logical(dataMask(tr,:)), :);
end
end
else
trialNum = unique(dataMask(:));
trialNum = trialNum(trialNum~=0);
n_tr = length(trialNum);
max_t = sum(dataMask(:)==mode(dataMask(dataMask~=0)));
alignedData = nan(n_tr, max_t, n_signals);
n_samps = zeros(n_tr,1);
for tr = 1:n_tr
[n_samps(tr), curr_tr] = max(sum(dataMask == trialNum(tr),2)); %Warning: only one trial can have indexes
if ~nanReversePadding
alignedData(tr,1:n_samps(tr),:) = data(curr_tr, dataMask(curr_tr,:) == trialNum(tr), :);
else
alignedData(tr,(end-n_samps(tr)+1):end,:) = data(curr_tr, dataMask(curr_tr,:) == trialNum(tr), :);
end
end
end
else
n_tr = length(trialNum);
n_samps = sum(logical(dataMask),2);
max_t = max(n_samps);
alignedData = nan(n_tr, max_t, n_signals);
for tr = 1:n_tr
if ~nanReversePadding
alignedData(tr,1:n_samps(tr),:) = data(trialNum(tr), logical(dataMask(tr,:)), :);
else
alignedData(tr,(end-n_samps(tr)+1):end,:) = data(trialNum(tr), logical(dataMask(tr,:)), :);
end
end
end
median_t = median(n_samps);
end