-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSD_openEphys.m
More file actions
1840 lines (1740 loc) · 76.7 KB
/
CSD_openEphys.m
File metadata and controls
1840 lines (1740 loc) · 76.7 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fast_CSD_64D.m
% Script for extracting LFP data from 64D probes on Intan
%
% Hard coded version for fast analysis during recordings by Ethan McBride
% Adapted from IntanLFP_CSD.m by Megan A Kirchgessner
% Which was adapted from LFPv2_latest from Bryan J. Hansen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
function CSD_openEphys(exp_path,probe)
%INPUTS: path, and '64D', '128DN_bottom', etc.
z_score=0;
combine_columns = 0;
% get necessary data
cd(exp_path)
if exist(sprintf('%s/data.mat',exp_path),'file')
load(sprintf('%s/data.mat',exp_path)) % data from intanphy2matlab.m
else
openEphys2matlab(exp_path); % data from intanphy2matlab.m
load(sprintf('%s/data.mat',exp_path))
end
%% memory map the amplifier file, filter, & downsample
first_half = exist(fullfile(exp_path,'100_CH1.continuous'),'file');
if first_half
contfile = fullfile(exp_path,'100_CH1.continuous');
else
contfile = fullfile(exp_path,'100_CH65.continuous');
end
[data, dataTime, dataInfo] = load_open_ephys_data_faster(contfile);
dataTime = dataTime./dataInfo(1).header.sampleRate; % uncomment if using load_open_ephys_data_faster
nsamps = length(dataTime);
amp_sr = dataInfo.header.sampleRate;
s=dir;
ch_names = {s(:).name};
nchans = length(cell2mat(cellfun(@(x) strfind(x,'_CH'),ch_names,'UniformOutput',false))); % count number of continuous channel files
v = zeros(nsamps,nchans); % preallocate matrix for raw data
v(:,1) = data;
clear data
for i = 2:nchans
contfile = sprintf('%s/100_CH%d.continuous',exp_path,i);
[v(:,i),~,~] = load_open_ephys_data_faster(contfile);
end
eventfile = fullfile(exp_path,'all_channels.events')
[events,eventTime,info] = load_open_ephys_data_faster(eventfile);
amp_sr = info.header.sampleRate;
LFPlow = 1000; %low pass forLFP freqs
[bf, af] = butter(2,(LFPlow/(amp_sr/2)), 'low');
% LFP=zeros(length(v(:,1))/20,64);
totaltime=0;
var_name = cell(1,nchans);
for ch = 1:nchans
if ch < 10
var_name{ch} = sprintf('LFP0%d',ch);
else
var_name{ch} = sprintf('LFP%d',ch);
end
end
cont=struct;
if strcmp(probe,'A')
field_file_name = 'fields_A';
elseif strcmp(probe,'B')
field_file_name = 'fields_B';
else
field_file_name = 'fields';
end
field_output_dir = fullfile(exp_path, field_file_name);
[~,message]=mkdir(field_output_dir);
cd(field_output_dir);
fileID2 = fopen('lfps.txt','w');
LN = size(v,1)/20; %downsample length
div = (amp_sr/20)/1000;
zx = 1:div:LN;
izx = floor(zx);
for i=1:nchans
tic;
% if i>1
% fprintf(fileID2,'\r\n');
% end
% ch=sprintf('%s%d', 'Processing channel: ', i);
% disp(ch);
if i<10
save_lfp=sprintf('%s%d','lfp0', i);
else
save_lfp=sprintf('%s%d','lfp', i);
end
if z_score == 1
lfp = LineFilterRmFFT(downsample(filtfilt(bf, af, zscore(v(:,i)),0),20),[59 62],0,0.05,0,amp_sr);
else
lfp = LineFilterRmFFT(downsample(filtfilt(bf, af, v(:,i)),20),[59 62],0,0.05,0,amp_sr);
end
% LFP(1:length(lfp),i) = lfp;
if i>1
fprintf(fileID2,'\r\n');
end
ch=sprintf('%s%d', 'Processing channel: ', i);
disp(ch);
if i<10
save_lfp=sprintf('%s%d','lfp0', i);
else
save_lfp=sprintf('%s%d','lfp', i);
end
cont(i).sig = lfp(izx);
% cont(i).sig = lfp;
cd(field_output_dir);
% save(save_lfp,'cont(i).sig');
fprintf(fileID2,save_lfp);
elapsedtime = toc;
totaltime = totaltime+elapsedtime;
timeleft = (64-i)*elapsedtime;
fprintf('%d channels complete, %.2f sec remaining',i,timeleft)
fprintf('\n')
clear lfp
end
fclose(fileID2);
% get probemap
p = eval(sprintf('probemap_%s_func',probe));
[~,inds] = sort(p.z); % sorts from bottom to top
chan_order = p.channels(inds);
chan_order = flipud(chan_order); % change from top to bottom
xx = flipud(p.x(inds));
nshanks = length(unique(p.shaft));
diff_cols = unique(xx);
x_dif = diff(diff_cols);
num_cols = length(diff_cols) - sum(diff(diff_cols)<5); % 64D probe has x vals at -20, -16, 0 , 16, and 20, but consider 16 and 20 the same
count = 1;
for i=1:length(x_dif)-sum(x_dif<5)+1
if x_dif(i) < 5
if length(diff_cols)==1
xcolvals{count-1} = [xcolvals{count-1}; diff_cols];
else
xcolvals{count} = diff_cols(1:2);
diff_cols(1:2) = [];
x_dif(i)=[];
end
else
xcolvals{count} = diff_cols(1);
diff_cols(1) = [];
end
count = count+1;
end
probemap = nan(ceil(nchans/num_cols),num_cols);
for i = 1:num_cols
col_chans = chan_order(ismember(xx,xcolvals{i}));
probemap(1:length(col_chans),i) = col_chans;
end
probemap = probemap+1; % change so that it stats from 1!!
if combine_columns == 1
spacing = max(abs(diff(p.z(inds))));
else
spacing = max(abs(diff(p.z(probemap(:,2)))));
end
pos = probemap(:);
pos(isnan(pos)) = [];
zvals = p.z(pos); % get corresponding vector of z values
shank = p.shaft(pos);
if combine_columns == 1
% for i=1:numel(probemap(:,1:2))-sum(isnan(probemap(:,1:2))) % only using channels in first two columns
for i=1:length(var_name)
sig{i}=sprintf('%s%d%s','=cont(',pos(i),').sig');
channel_reorder = evalc([var_name{i}, sig{i}]); % LFPs are ordered from top to bottom!
end
else
for i=1:length(var_name)
sig{i}=sprintf('%s%d%s','=cont(',pos(i),').sig');
channel_reorder = evalc([var_name{i}, sig{i}]); % LFPs are ordered from top to bottom!
end
end
elapsedtime = toc;
totaltime = totaltime+elapsedtime;
fprintf('Complete! total elapsed time was %.2f seconds',totaltime)
fprintf('\n')
%% find photo pulses
disp('Find timing using PD pulses to est. the Evoked Response Potential (ERP)')
tic
new_re=zeros(1,length(re));
for i=1:length(re)-1
pdiff(i)=(re(i+1))-(re(i));
if pdiff(i)>3.5 && pdiff(i)<4.1
new_re(1,i)=re(i+1);
else
new_re(1,i)=0;
end
end
disp('Identify timestamps of photodiode')
new_re(new_re==0)=[];% timestamps for the end of the photo
for i=1:length(new_re)% the first two and last two pulses are removed
%%% this sections is important because it est the timing for the
%%% rest of the experiment
%%% 100msec before flip, 400msec after
% st_time(i,:)=(new_re(i)-3.00); end_time(i,:)=new_re(i)-1.00; %dark->light
% st_time_down(i,:)=(new_re(i)-1.00); end_time_down(i,:)=new_re(i)+1; %light->dark
st_time(i,:)=(new_re(i)-2.50); end_time(i,:)=new_re(i)-1.50; %dark->light
st_time_down(i,:)=(new_re(i)-0.50); end_time_down(i,:)=new_re(i)+0.50; %light->dark
end
for i=1:length(st_time)
trials (i,1) = find(time_index>=st_time(i)&time_index<=end_time(i),1,'first'); % in samples, starting from 1
trials (i,2) = find(time_index>=(time_index(trials(i,1)))&time_index<=(time_index(trials(i,1))+2.000),1,'last');
trial_time(i,:)= trials(i,1):1:trials(i,1)+1000;
trialsd(i,1) = find(time_index>=st_time_down(i)&time_index<=end_time_down(i),1,'first'); % in samples, starting from 1
trialsd(i,2) = find(time_index>=(time_index(trialsd(i,1)))&time_index<=(time_index(trialsd(i,1))+2.000),1,'last');
triald_time(i,:)= trialsd(i,1):1:trialsd(i,1)+1000;
end
timing=(time_index(trial_time(1,:))-time_index(trial_time(1,1))-0.5);
timingd=(time_index(triald_time(1,:))-time_index(triald_time(1,1))-0.5);
%% Largely redundant section for movement trials, may remove
% disp('Find the movement data and determine if the mouse was running or stationary')
ts = [ ];
% assumes there will never be more than 1000 events in an interval
maxevents = 1000;
% assumes a block will never be longer than 10000 seconds
maxtime = 10000;
% steps through block in 100 second intervals
steps = maxtime / 100;
mmvt=0;
for i=1:length(trials)
if mmvt == 0
mmvt_count(i) = 0;
else
mmvt_trials{i,:} = mmvt(1,trials(i,1):trials(i,2));
mmvt_count(i,:) = length(find(mmvt_trials{i}==1)); % added by RK
end
end
counter1=0;
counter2=0;
for i=1:length(mmvt_count)
if mmvt_count(i)>=1 %>=2
counter1=counter1+1;
trial_running(counter1)=i;
elseif mmvt_count(i)<1 %<=2
counter2=counter2+1;
trial_stationary(counter2)=i;
end
end
for i=1:length(trial_stationary)
trials2(i,:)= trials(trial_stationary(i),1):1:trials(trial_stationary(i),1)+1000;
trials2d(i,:)= trialsd(trial_stationary(i),1):1:trialsd(trial_stationary(i),1)+1000;
end
%% Remove trials with high diff from median!
disp('Removing noisy trials')
multiplier = 2; %stdev cutoff for trial removal
% DIFF = bsxfun(@minus,LFP01,median(LFP01,1));
% ERP01(std(bsxfun(@minus,ERP01,median(ERP01,1)),0,2)>multiplier*median(std(bsxfun(@minus,ERP01,median(ERP01,1)),0,2)),:)=[];
% ERP01=mean(ERP01,1);
for i=1:length(cont)
% ERP01 = LFP01(trials2);
% ERP01 = mean(ERP01(std(bsxfun(@minus,LFP01(trials2),median(LFP01(trials2),1)),0,2)<=multiplier*median(std(bsxfun(@minus,LFP01(trials2),median(LFP01(trials2),1)),0,2)),:),1);
if i<10
eval(['ERP0' num2str(i) '= LFP0' num2str(i) '(trials2);']);
eval(['ERP0' num2str(i) '= mean(ERP0' num2str(i) '(std(bsxfun(@minus,LFP0' num2str(i) '(trials2),median(LFP0'...
num2str(i) '(trials2),1)),0,2)<=multiplier*median(std(bsxfun(@minus,LFP0'...
num2str(i) '(trials2),median(LFP0' num2str(i) '(trials2),1)),0,2)),:),1);']);
eval(['ERPd0' num2str(i) '= LFP0' num2str(i) '(trials2d);']);
eval(['ERPd0' num2str(i) '= mean(ERPd0' num2str(i) '(std(bsxfun(@minus,LFP0' num2str(i) '(trials2d),median(LFP0'...
num2str(i) '(trials2d),1)),0,2)<=multiplier*median(std(bsxfun(@minus,LFP0'...
num2str(i) '(trials2d),median(LFP0' num2str(i) '(trials2d),1)),0,2)),:),1);']);
else
eval(['ERP' num2str(i) '= LFP' num2str(i) '(trials2);']);
eval(['ERP' num2str(i) '= mean(ERP' num2str(i) '(std(bsxfun(@minus,LFP' num2str(i) '(trials2),median(LFP'...
num2str(i) '(trials2),1)),0,2)<=multiplier*median(std(bsxfun(@minus,LFP'...
num2str(i) '(trials2),median(LFP' num2str(i) '(trials2),1)),0,2)),:),1);']);
eval(['ERPd' num2str(i) '= LFP' num2str(i) '(trials2d);']);
eval(['ERPd' num2str(i) '= mean(ERPd' num2str(i) '(std(bsxfun(@minus,LFP' num2str(i) '(trials2d),median(LFP'...
num2str(i) '(trials2d),1)),0,2)<=multiplier*median(std(bsxfun(@minus,LFP'...
num2str(i) '(trials2d),median(LFP' num2str(i) '(trials2d),1)),0,2)),:),1);']);
end
end
if strfind(probe,'64D')
ERP_shk1=vertcat(ERP01,ERP02,ERP03,ERP04,ERP05,ERP06,ERP07,ERP08,ERP09,ERP10,ERP11,ERP12,ERP13,ERP14,ERP15,ERP16,ERP17,ERP18,ERP19,ERP20,ERP21);
ERP_shk2= vertcat(ERP22,ERP23,ERP24,ERP25,ERP26,ERP27,ERP28,ERP29,ERP30,ERP31,ERP32,ERP33,ERP34,ERP35,ERP36,ERP37,ERP38,ERP39,ERP40,ERP41,ERP42,ERP43);
ERP_shk3 = vertcat(ERP44,ERP45,ERP46,ERP47,ERP48,ERP49,ERP50,ERP51,ERP52,ERP53,ERP54,ERP55,ERP56,ERP57,ERP58,ERP59,ERP60,ERP61,ERP62,ERP63,ERP64);
% ERP_shk1=(aux_shk1/(1*10^8))*(1*10^6);% Scale factor for ERP
% ERP_shk2=(aux_shk2/(1*10^8))*(1*10^6);% Scale factor for ERP
% ERP_shk3=(aux_shk3/(1*10^8))*(1*10^6);% Scale factor for ERP
ERPd_shk1=vertcat(ERPd01,ERPd02,ERPd03,ERPd04,ERPd05,ERPd06,ERPd07,ERPd08,ERPd09,ERPd10,ERPd11,ERPd12,ERPd13,ERPd14,ERPd15,ERPd16,ERPd17,ERPd18,ERPd19,ERPd20,ERPd21);
ERPd_shk2=vertcat(ERPd22,ERPd23,ERPd24,ERPd25,ERPd26,ERPd27,ERPd28,ERPd29,ERPd30,ERPd31,ERPd32,ERPd33,ERPd34,ERPd35,ERPd36,ERPd37,ERPd38,ERPd39,ERPd40,ERPd41,ERPd42,ERPd43);
ERPd_shk3= vertcat(ERPd44,ERPd45,ERPd46,ERPd47,ERPd48,ERPd49,ERPd50,ERPd51,ERPd52,ERPd53,ERPd54,ERPd55,ERPd56,ERPd57,ERPd58,ERPd59,ERPd60,ERPd61,ERPd62,ERPd63,ERPd64);
% ERPd_shk1=(aux_shk1d/(1*10^8))*(1*10^6);% Scale factor for ERP <<<< MAYBE CHANGE THIS
% ERPd_shk2=(aux_shk2d/(1*10^8))*(1*10^6);% Scale factor for ERP
% ERPd_shk3=(aux_shk3d/(1*10^8))*(1*10^6);% Scale factor for ERP
elseif strfind(probe,'128AN')
ERP_shk1=vertcat(ERP01,ERP02,ERP03,ERP04,ERP05,ERP06,ERP07,ERP08,ERP09,ERP10,ERP11,ERP12,ERP13,ERP14,ERP15,ERP16,ERP17,ERP18,ERP19,ERP20,ERP21);
ERP_shk2= vertcat(ERP22,ERP23,ERP24,ERP25,ERP26,ERP27,ERP28,ERP29,ERP30,ERP31,ERP32,ERP33,ERP34,ERP35,ERP36,ERP37,ERP38,ERP39,ERP40,ERP41,ERP42,ERP43);
ERP_shk3 = vertcat(ERP44,ERP45,ERP46,ERP47,ERP48,ERP49,ERP50,ERP51,ERP52,ERP53,ERP54,ERP55,ERP56,ERP57,ERP58,ERP59,ERP60,ERP61,ERP62,ERP63,ERP64);
ERP_shk4=vertcat(ERP65,ERP66,ERP67,ERP68,ERP69,ERP70,ERP71,ERP72,ERP73,ERP74,ERP75,ERP76,ERP77,ERP78,ERP79,ERP80,ERP81,ERP82,ERP83,ERP84,ERP85);
ERP_shk5= vertcat(ERP86,ERP87,ERP88,ERP89,ERP90,ERP91,ERP92,ERP93,ERP94,ERP95,ERP96,ERP97,ERP98,ERP99,ERP100,ERP101,ERP102,ERP103,ERP104,ERP105,ERP106,ERP107);
ERP_shk6 = vertcat(ERP108,ERP109,ERP110,ERP111,ERP112,ERP113,ERP114,ERP115,ERP116,ERP117,ERP118,ERP119,ERP120,ERP121,ERP122,ERP123,ERP124,ERP125,ERP126,ERP127,ERP128);
ERPd_shk1=vertcat(ERPd01,ERPd02,ERPd03,ERPd04,ERPd05,ERPd06,ERPd07,ERPd08,ERPd09,ERPd10,ERPd11,ERPd12,ERPd13,ERPd14,ERPd15,ERPd16,ERPd17,ERPd18,ERPd19,ERPd20,ERPd21);
ERPd_shk2=vertcat(ERPd22,ERPd23,ERPd24,ERPd25,ERPd26,ERPd27,ERPd28,ERPd29,ERPd30,ERPd31,ERPd32,ERPd33,ERPd34,ERPd35,ERPd36,ERPd37,ERPd38,ERPd39,ERPd40,ERPd41,ERPd42,ERPd43);
ERPd_shk3= vertcat(ERPd44,ERPd45,ERPd46,ERPd47,ERPd48,ERPd49,ERPd50,ERPd51,ERPd52,ERPd53,ERPd54,ERPd55,ERPd56,ERPd57,ERPd58,ERPd59,ERPd60,ERPd61,ERPd62,ERPd63,ERPd64);
ERPd_shk4=vertcat(ERPd65,ERPd66,ERPd67,ERPd68,ERPd69,ERPd70,ERPd71,ERPd72,ERPd73,ERPd74,ERPd75,ERPd76,ERPd77,ERPd78,ERPd79,ERPd80,ERPd81,ERPd82,ERPd83,ERPd84,ERPd85);
ERPdd_shk5= vertcat(ERPd86,ERPd87,ERPd88,ERPd89,ERPd90,ERPd91,ERPd92,ERPd93,ERPd94,ERPd95,ERPd96,ERPd97,ERPd98,ERPd99,ERPd100,ERPd101,ERPd102,ERPd103,ERPd104,ERPd105,ERPd106,ERPd107);
ERPdd_shk6 = vertcat(ERPd108,ERPd109,ERPd110,ERPd111,ERPd112,ERPd113,ERPd114,ERPd115,ERPd116,ERPd117,ERPd118,ERPd119,ERPd120,ERPd121,ERPd122,ERPd123,ERPd124,ERPd125,ERPd126,ERPd127,ERPd128);
end
% trials2 = floor(trials2/(amp_sr/1000)); % downsample
%% Get the mean of ERP and ERPd
for i=1:length(ERP_shk1(:,1))
ERP_shk1_testmean(i,:) = mean([ERP_shk1(i,:);ERPd_shk1(i,:)]);
end
% ERPd_shk1=(aux_shk1d/(1*10^8))*(1*10^6);
ERP_shk1 = ERP_shk1_testmean;
% if combine_columns==0
for i=1:length(ERP_shk2(:,1))
ERP_shk2_testmean(i,:) = mean([ERP_shk2(i,:);ERPd_shk2(i,:)]);
end
for i=1:length(ERP_shk3(:,1))
ERP_shk3_testmean(i,:) = mean([ERP_shk3(i,:);ERPd_shk3(i,:)]);
end
ERP_shk2 = ERP_shk2_testmean;
ERP_shk3 = ERP_shk3_testmean;
% end
%% Plot and clean up ERP figures %%%MEAN NORMALIZATION
probemap_chrem = probemap; %map of removed channels = nan
% probemapd_chrem = probemap;
[ERP_shk1 probemap_chrem]=clean_ERP_MAK(ERP_shk1,timing,probemap_chrem,1);
ERP_shk1_nonan=ERP_shk1;
[ERP_shk2 probemap_chrem]=clean_ERP_MAK(ERP_shk2,timing,probemap_chrem,2);
[ERP_shk3 probemap_chrem]=clean_ERP_MAK(ERP_shk3,timing,probemap_chrem,3);
% remove nan's by replacing with the average of the nearest neighbors
%check if there are non-nan channels adjacent
nanidx1 = find(isnan(ERP_shk1(:,1)));
nonnanidx1 = find(~isnan(ERP_shk1(:,1)));
adjidx1 = zeros(length(nanidx1),2);
for i=1:length(nanidx1)
temp = nonnanidx1-nanidx1(i);%distance from nan chan to other chans
% if nanidx1(i) == 1 %if it's channel 1
% adjidx1(i,1) = nanidx1(i);
% else
[tempmin tempidx] = min(abs(temp(temp<0)));%find first min distance
if ~isempty(tempmin)
adjidx1(i,1) = nonnanidx1(tempidx);
temp(1:tempidx) = nan; %make first min nan
else
adjidx1(i,1) = nan;
end
% end
% if nanidx1(i) == length(ERP_shk1(:,1)) %if it's the last channel
% adjidx1(i,2) = nanidx1(i);
% else
[tempmin tempidx] = min(abs(temp));%find second min distance
if ~isempty(tempmin) && ~isnan(tempmin)
adjidx1(i,2) = nonnanidx1(tempidx);
else
adjidx1(i,2) = nan;
end
% end
end
%find the distance to the nearest non-nan channels and then do weighted averages!
%shank1
for i=1:length(nanidx1)
if isnan(adjidx1(i,1)) %if channels at end of probe are removed
ERP_shk1_nonan(nanidx1(i),:) = ERP_shk1(adjidx1(i,2),:); %just duplicate nearest neighbor
elseif isnan(adjidx1(i,2))
ERP_shk1_nonan(nanidx1(i),:) = ERP_shk1(adjidx1(i,1),:);
elseif adjidx1(i,2)-adjidx1(i,1)>=2 %if nan chan is between channels
d1 = nanidx1(i)-adjidx1(i,1); %distances to nearest non-nan channels
d2 = adjidx1(i,2)-nanidx1(i);
if d1 == d2
ERP_shk1_nonan(nanidx1(i),:) = mean([ERP_shk1(adjidx1(i,1),:);ERP_shk1(adjidx1(i,2),:)],1);
else
%WEIGHTED AVERAGE....NOT SURE I DID THIS RIGHT!?!
ERP_shk1_nonan(nanidx1(i),:) = (ERP_shk1(adjidx1(i,1),:)*(1/d1) + ERP_shk1(adjidx1(i,2),:)*(1/d2))/(1/d1+1/d2);
end
elseif adjidx1(i,2)-adjidx1(i,1)==1 %if nan chan is at the end of the probe
if nanidx1(i) == 1
ERP_shk1_nonan(nanidx1(i),:) = ERP_shk1(adjidx1(i,2),:); %just duplicate nearest neighbor
elseif nanidx1(i) == length(ERP_shk1(:,1))
ERP_shk1_nonan(nanidx1(i),:) = ERP_shk1(adjidx1(i,1),:);
end
elseif adjidx1(i,2)-adjidx1(i,1)==0
%%%
end
end
% normalize ERPS i.e. subtract common noise (from monitor?)<< but already
% did this!! (MAK)
% if combine_columns == 0
ERP_shk1_mean = nanmean(ERP_shk1_nonan,1);
% for i = 1:length(ERP_shk1_nonan(:,1))
% if isnan(ERP_shk1_nonan(i,1))
% ERP_shk1_norm(i,1:length(ERP_shk1(1,:))) = 0;
% else
% ERP_shk1_norm(i,1:length(ERP_shk1(1,:))) = ERP_shk1_nonan(i,:) - ERP_shk1_mean;
% end
% end
%try normalizing each column before combining
% else
% ERP_shk1_mean1 = nanmedian(ERP_shk1_nonan(pos_col==1),1);
% ERP_shk1_mean2 = nanmedian(ERP_shk1_nonan(pos_col==2),1);
%
% for i = 1:length(ERP_shk1_nonan(:,1))
% if isnan(ERP_shk1_nonan(i,1))
% ERP_shk1_norm(i,1:length(ERP_shk1(1,:))) = 0;
% else
% if pos_col(i)==1
% ERP_shk1_norm(i,1:length(ERP_shk1(1,:))) = ERP_shk1_nonan(i,:) - ERP_shk1_mean1;
% elseif pos_col(i)==0
% ERP_shk1_norm(i,1:length(ERP_shk1(1,:))) = ERP_shk1_nonan(i,:) - ERP_shk1_mean2;
% end
% end
% end
%
% end
% if combine_columns==0
% [ERPd_shk1 probemapd_chrem]=clean_ERP_EGM(ERPd_shk1,timing,probemapd_chrem,1);
% [ERPd_shk2 probemapd_chrem]=clean_ERP_EGM(ERPd_shk2,timing,probemapd_chrem,2);
% [ERPd_shk3 probemapd_chrem]=clean_ERP_EGM(ERPd_shk3,timing,probemapd_chrem,3);
% for i=1:length(ERP_shk1(:,1))
% ERP_shk1_both(i,:) = nanmedian([ERP_shk1(i,:);ERPd_shk1(i,:)]);
% end
% for i=1:length(ERP_shk2(:,1))
% ERP_shk2_both(i,:) = nanmedian([ERP_shk2(i,:);ERPd_shk2(i,:)]);
% end
% for i=1:length(ERP_shk3(:,1))
% ERP_shk3_both(i,:) = nanmedian([ERP_shk3(i,:);ERPd_shk3(i,:)]);
% end
%
% ERP_shk1 = ERP_shk1_both;
% ERP_shk2 = ERP_shk2_both;
% ERP_shk3 = ERP_shk3_both;
% % normalize ERPS i.e. subtract common noise (from monitor?)
% ERP_shk1_mean = nanmean(ERP_shk1,1);
% ERP_shk2_mean = nanmean(ERP_shk2,1);
% ERP_shk3_mean = nanmean(ERP_shk3,1);
% ERP_shk_all = [ERP_shk1; ERP_shk2; ERP_shk3];
% ERP_shk_all_mean = nanmean(ERP_shk_all,1);
% remove nan's by replacing with the average of the nearest neighbors
%check if there are non-nan channels adjacent
ERP_shk2_nonan=ERP_shk2;
ERP_shk3_nonan=ERP_shk3;
nanidx2 = find(isnan(ERP_shk2(:,1)));
nonnanidx2 = find(~isnan(ERP_shk2(:,1)));
adjidx2 = zeros(length(nanidx2),2);
for i=1:length(nanidx2)
temp = nonnanidx2-nanidx2(i);%distance from nan chan to other chans
[tempmin tempidx] = min(abs(temp(temp<0)));%find first min distance
if ~isempty(tempmin)
adjidx2(i,1) = nonnanidx2(tempidx);
temp(1:tempidx) = nan; %make first min nan
else
adjidx2(i,1) = nan;
end
[tempmin tempidx] = min(abs(temp));%find second min distance
if ~isempty(tempmin) && ~isnan(tempmin)
adjidx2(i,2) = nonnanidx2(tempidx);
else
adjidx2(i,2) = nan;
end
end
nanidx3 = find(isnan(ERP_shk3(:,1)));
nonnanidx3 = find(~isnan(ERP_shk3(:,1)));
adjidx3 = zeros(length(nanidx3),2);
for i=1:length(nanidx3)
temp = nonnanidx3-nanidx3(i);%distance from nan chan to other chans
[tempmin tempidx] = min(abs(temp(temp<0)));%find first min distance
if ~isempty(tempmin)
adjidx3(i,1) = nonnanidx3(tempidx);
temp(1:tempidx) = nan; %make first min nan
else
adjidx3(i,1) = nan;
end
[tempmin tempidx] = min(abs(temp));%find second min distance
if ~isempty(tempmin) && ~isnan(tempmin)
adjidx3(i,2) = nonnanidx3(tempidx);
else
adjidx3(i,2) = nan;
end
end
%find the distance to the nearest non-nan channels and then do weighted averages!
% %if there are many consecutive nan channels just delete them!
% consec1 = nanidx1(find(diff(nanidx1)==1));
% if ~isempty(consec1)
% ERP_shk1_nonan([consec1; consec1(end)+1],:)=[];
% end
%shank2
for i=1:length(nanidx2)
if isnan(adjidx2(i,1)) %if channels at end of probe are removed
ERP_shk2_nonan(nanidx2(i),:) = ERP_shk2(adjidx2(i,2),:); %just duplicate nearest neighbor
elseif isnan(adjidx2(i,2))
ERP_shk2_nonan(nanidx2(i),:) = ERP_shk2(adjidx2(i,1),:);
elseif adjidx2(i,2)-adjidx2(i,1)>=2 %if nan chan is between channels
d1 = nanidx2(i)-adjidx2(i,1); %distances to nearest non-nan channels
d2 = adjidx2(i,2)-nanidx2(i);
if d1 == d2
ERP_shk2_nonan(nanidx2(i),:) = mean([ERP_shk2(adjidx2(i,1),:);ERP_shk2(adjidx2(i,2),:)],1);
else
%WEIGHTED AVERAGE....NOT SURE I DID THIS RIGHT!?!
ERP_shk2_nonan(nanidx2(i),:) = (ERP_shk2(adjidx2(i,1),:)*(1/d1) + ERP_shk2(adjidx2(i,2),:)*(1/d2))/(1/d1+1/d2);
end
else %if nan chan is at the end of the probe
if nanidx2(i) == 1
ERP_shk2_nonan(nanidx2(i),:) = ERP_shk2(adjidx2(i,2),:); %just duplicate nearest neighbor
elseif nanidx2(i) == length(ERP_shk2(:,1))
ERP_shk2_nonan(nanidx2(i),:) = ERP_shk2(adjidx2(i,1),:);
end
end
end
% %if there are many consecutive nan channels just delete them!
% consec2 = nanidx2(find(diff(nanidx2)==1));
% if ~isempty(consec2)
% ERP_shk2_nonan([consec2; consec2(end)+1],:)=[];
% end
%shank3
for i=1:length(nanidx3)
if isnan(adjidx3(i,1)) %if channels at end of probe are removed
ERP_shk3_nonan(nanidx3(i),:) = ERP_shk3(adjidx3(i,2),:); %just duplicate nearest neighbor
elseif isnan(adjidx3(i,2))
ERP_shk3_nonan(nanidx3(i),:) = ERP_shk3(adjidx3(i,1),:);
elseif adjidx3(i,2)-adjidx3(i,1)>=2 %if nan chan is between channels
d1 = nanidx3(i)-adjidx3(i,1); %distances to nearest non-nan channels
d2 = adjidx3(i,2)-nanidx3(i);
if d1 == d2
ERP_shk3_nonan(nanidx3(i),:) = mean([ERP_shk3(adjidx3(i,1),:);ERP_shk3(adjidx3(i,2),:)],1);
else
%WEIGHTED AVERAGE....NOT SURE I DID THIS RIGHT!?!
ERP_shk3_nonan(nanidx3(i),:) = (ERP_shk3(adjidx3(i,1),:)*(1/d1) + ERP_shk3(adjidx3(i,2),:)*(1/d2))/(1/d1+1/d2);
end
else %if nan chan is at the end of the probe
if nanidx3(i) == 1
ERP_shk3_nonan(nanidx3(i),:) = ERP_shk3(adjidx3(i,2),:); %just duplicate nearest neighbor
elseif nanidx3(i) == length(ERP_shk3(:,1))
ERP_shk3_nonan(nanidx3(i),:) = ERP_shk3(adjidx3(i,1),:);
end
end
end
% consec3 = nanidx3(find(diff(nanidx3)==1));
% if ~isempty(consec3)
% ERP_shk3_nonan([consec3; consec3(end)+1],:)=[];
% end
% normalize ERPS i.e. subtract common noise (from monitor?)
ERP_shk2_mean = nanmean(ERP_shk2_nonan,1);
ERP_shk3_mean = nanmean(ERP_shk3_nonan,1);
ERP_shk_all = [ERP_shk1_nonan; ERP_shk2_nonan; ERP_shk3_nonan];
ERP_shk_all_mean = nanmean(ERP_shk_all,1);
% for i = 1:length(ERP_shk2_nonan(:,1))
% if isnan(ERP_shk2_nonan(i,1))
% ERP_shk2_norm(i,1:length(ERP_shk2(1,:))) = 0;
% else
% ERP_shk2_norm(i,1:length(ERP_shk2(1,:))) = ERP_shk2_nonan(i,:) - ERP_shk2_mean;
% end
% end
% for i = 1:length(ERP_shk3_nonan(:,1))
% if isnan(ERP_shk3_nonan(i,1))
% ERP_shk3_norm(i,1:length(ERP_shk3(1,:))) = 0;
% else
% ERP_shk3_norm(i,1:length(ERP_shk3(1,:))) = ERP_shk3_nonan(i,:) - ERP_shk3_mean;
% end
% end
% ERP_columns12([1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41],:) = ERP_shk1_norm;
% ERP_columns12([2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,43],:) = ERP_shk2_norm;
% end
%%%FOR NOW JUST MAKING REMOVED CHANNELS == 0 ... THERE'S PROBABLY A BETTER WAY!!
%% The final ERP figure is sent to the screen to view and now the next section deals
% with the CSD plotter and average analysis
% pause(5);
file_name = 'CSD_LFPdata';
save (file_name);
disp ('saving mat')
HC=CSDplotter;
disp ('Use CSD plotter')
clc
pause(20)
disp ('ERP: normal LFP time series w/o moving trials')
reply = input('Ready to continue? Y/N: ','s');
if reply == 'Y';
close all;
disp ('Open CSD matrix file for Shank 1')
filename = uigetfile;
load (filename);
CSD_matrix1=CSD_matrix;
fprintf('%s%s','Filename: ',filename);
close all
clc
disp ('Plot final ERP and CSD side by side')
X2=timing.*1000;
clear CSD_matrix
if combine_columns == 0
disp ('Open CSD matrix file for Shank 2')
filename = uigetfile;
load (filename);
CSD_matrix2=CSD_matrix;
fprintf('%s%s','Filename: ',filename);
close all
clc
disp ('Open CSD matrix file for Shank 3')
filename = uigetfile;
load (filename);
CSD_matrix3=CSD_matrix;
fprintf('%s%s','Filename: ',filename);
close all
clc
end
disp ('Plot final ERP and CSD side by side')
%% Plot ERPs
FontName = 'MyriadPro-Regular'; % or choose any other font
FontSize = 14;
figure_width = 14;
figure_height = 10;
figuresVisible = 'on'; % 'off' for non displayed plots (will still be exported)
ERP_fig1 = figure('Name','ERP1');
set(ERP_fig1,'Visible', figuresVisible)
set(ERP_fig1, 'units', 'centimeters', 'pos', [5 5 figure_width figure_height])
set(ERP_fig1, 'PaperPositionMode', 'auto');
set(ERP_fig1, 'Color', [1 1 1]); % Sets figure background
set(ERP_fig1, 'Color', [1 1 1]); % Sets axes background
hsp = subplot(1,1,1, 'Parent', ERP_fig1);
set(hsp,'Position',[0.15 0.17 0.75 0.80]);
plot (X2, ERP_shk1_nonan');
axis on; % display axis
axis tight; % no white borders
set(gca, ...
'Box' , 'off' , ...
'TickDir' , 'out' , ...
'TickLength' , [.015 .015] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'XGrid' , 'off' , ...
'YGrid' , 'off' , ...
'XColor' , [.0 .0 .0], ...
'YColor' , [.0 .0 .0], ...
'LineWidth' , 0.6 );
set(gca,'Xlim',[-25 225]);
yaxis=ylim;
set(gca,'XTickLabel',[0 200], 'Xtick', [0 200])
prestim_offset_y = yaxis(1):1:yaxis(2);
prestim_offset_t = ones(1, length(prestim_offset_y))*0;
hold on;plot(prestim_offset_t, prestim_offset_y, 'k','linewidth', 1);
xLabelText = 'Time from stimulus onset (ms)'; % greek letters in LaTeX Syntax
yLabelText = 'Amplitude (uV)';
% save handles to set up label properties
hXLabel = xlabel(xLabelText);
hYLabel = ylabel(yLabelText);
set([gca, hXLabel, hYLabel], ...
'FontSize' , FontSize , ...
'FontName' , FontName);
fig_title=sprintf('%s','ERP 1 ');
set(gca,'Layer', 'top');
drawnow
%export_fig (fig_title, '-pdf')
% export_fig(ERP_fig1, '-png','-r600','-zbuffer');
if combine_columns==0
FontName = 'MyriadPro-Regular'; % or choose any other font
FontSize = 14;
figure_width = 14;
figure_height = 10;
figuresVisible = 'on'; % 'off' for non displayed plots (will still be exported)
ERP_fig2 = figure('Name','ERP2');
set(ERP_fig2,'Visible', figuresVisible)
set(ERP_fig2, 'units', 'centimeters', 'pos', [5 5 figure_width figure_height])
set(ERP_fig2, 'PaperPositionMode', 'auto');
set(ERP_fig2, 'Color', [1 1 1]); % Sets figure background
set(ERP_fig2, 'Color', [1 1 1]); % Sets axes background
hsp = subplot(1,1,1, 'Parent', ERP_fig2);
set(hsp,'Position',[0.19 0.19 0.75 0.80]);
plot (X2, ERP_shk2_nonan');
axis on; % display axis
axis tight; % no white borders
set(gca, ...
'Box' , 'off' , ...
'TickDir' , 'out' , ...
'TickLength' , [.015 .015] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'XGrid' , 'off' , ...
'YGrid' , 'off' , ...
'XColor' , [.0 .0 .0], ...
'YColor' , [.0 .0 .0], ...
'LineWidth' , 0.6 );
set(gca,'Xlim',[-25 225]);
yaxis=ylim;
set(gca,'XTickLabel',[0 200], 'Xtick', [0 200])
prestim_offset_y = yaxis(1):1:yaxis(2);
prestim_offset_t = ones(1, length(prestim_offset_y))*0;
hold on;plot(prestim_offset_t, prestim_offset_y, 'k','linewidth', 1);
xLabelText = 'Time from stimulus onset (ms)'; % greek letters in LaTeX Syntax
yLabelText = 'Amplitude (uV)';
% save handles to set up label properties
hXLabel = xlabel(xLabelText);
hYLabel = ylabel(yLabelText);
set([gca, hXLabel, hYLabel], ...
'FontSize' , FontSize , ...
'FontName' , FontName);
fig_title=sprintf('%s','ERP 2 ');
set(gca,'Layer', 'top');
drawnow
% export_fig (fig_title, '-pdf')
% export_fig (ERP_fig2, '-png','-r600','-opengl')
FontName = 'MyriadPro-Regular'; % or choose any other font
FontSize = 14;
figure_width = 14;
figure_height = 10;
figuresVisible = 'on'; % 'off' for non displayed plots (will still be exported)
ERP_fig3 = figure('Name','ERP3');
set(ERP_fig3,'Visible', figuresVisible)
set(ERP_fig3, 'units', 'centimeters', 'pos', [5 5 figure_width figure_height])
set(ERP_fig3, 'PaperPositionMode', 'auto');
set(ERP_fig3, 'Color', [1 1 1]); % Sets figure background
set(ERP_fig3, 'Color', [1 1 1]); % Sets axes background
hsp = subplot(1,1,1, 'Parent', ERP_fig3);
set(hsp,'Position',[0.19 0.19 0.75 0.80]);
plot (X2, ERP_shk3_nonan');
axis on; % display axis
axis tight; % no white borders
set(gca, ...
'Box' , 'off' , ...
'TickDir' , 'out' , ...
'TickLength' , [.015 .015] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'XGrid' , 'off' , ...
'YGrid' , 'off' , ...
'XColor' , [.0 .0 .0], ...
'YColor' , [.0 .0 .0], ...
'LineWidth' , 0.6 );
set(gca,'Xlim',[-25 225]);
yaxis=ylim;
set(gca,'XTickLabel',[0 200], 'Xtick', [0 200])
prestim_offset_y = yaxis(1):1:yaxis(2);
prestim_offset_t = ones(1, length(prestim_offset_y))*0;
hold on;plot(prestim_offset_t, prestim_offset_y, 'k','linewidth', 1);
xLabelText = 'Time from stimulus onset (ms)'; % greek letters in LaTeX Syntax
yLabelText = 'Amplitude (uV)';
% save handles to set up label properties
hXLabel = xlabel(xLabelText);
hYLabel = ylabel(yLabelText);
set([gca, hXLabel, hYLabel], ...
'FontSize' , FontSize , ...
'FontName' , FontName);
fig_title=sprintf('%s','ERP 3 ');
set(gca,'Layer', 'top');
drawnow
end
% export_fig (fig_title, '-pdf')
% export_fig (ERP_fig3, '-png','-r600','-opengl')
%% Plot ERPs stacked
if combine_columns==1
channels= fliplr([1:1:size(ERP_shk1,1)]);
FontName = 'MyriadPro-Regular'; % or choose any other font
FontSize = 14;
figure_width = 14;
figure_height = 14;
figuresVisible = 'on'; % 'off' for non displayed plots (will still be exported)
ERP_stacked=figure('Name','ERP_stacked');% figure('units', 'normalized', 'outerposition', [0 0 1 1]);
set(ERP_stacked, 'units', 'centimeters', 'pos', [5 5 figure_width figure_height])
set(ERP_stacked, 'PaperPositionMode', 'auto');
set(ERP_stacked, 'Color', [1 1 1]); % Sets figure background
set(ERP_stacked, 'Color', [1 1 1]); % Sets axes background
hsp = subplot(1,1,1, 'Parent', ERP_stacked);
set(hsp,'Position',[0.15 0.17 0.75 0.80]);
depth = 0;
depth_spacing = 25;
max_y = 0;
hold all;
% for each channel
chan_legends = {};
for j=1:length(channels);
for i = channels(j)
averaged_ERP= ERP_shk1_nonan(i,:);
plot(X2, averaged_ERP*depth_spacing + depth,'LineWidth',2);
max_y= max_y + max(averaged_ERP);
depth= depth + depth_spacing;
chan_legends= [chan_legends, num2str(i)];
end
end
yaxis=ylim;
set(gca,'Ylim',[-depth_spacing depth_spacing*length(channels)])
set(gca, 'ytick', [0:depth_spacing:(depth_spacing*length(channels))-depth_spacing],'tickdir','out','yticklabel',[channels]);
axis on; % display axis
set(gca, ...
'Box' , 'off' , ...
'TickDir' , 'out' , ...
'TickLength' , [0 0] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'XGrid' , 'off' , ...
'YGrid' , 'off' , ...
'XColor' , [.0 .0 .0], ...
'YColor' , [.0 .0 .0], ...
'LineWidth' , 0.6 );
set(gca,'Xlim',[-25 225]);
set(gca,'XTickLabel',[0 200], 'Xtick', [0 200])
xLabelText = 'Time from stimulus onset (ms)'; % greek letters in LaTeX Syntax
yLabelText = 'Electrode number (Sup-->Deep)';
% save handles to set up label properties
hXLabel = xlabel(xLabelText);
hYLabel = ylabel(yLabelText);
set([gca, hXLabel, hYLabel], ...
'FontSize' , FontSize , ...
'FontName' , FontName);
prestim_offset_y = yaxis(1):1:yaxis(2);
prestim_offset_t = ones(1, length(prestim_offset_y))*0;
plot(prestim_offset_t, prestim_offset_y, 'k', 'linewidth',2);
% poststimulus onset
else
left_channels= fliplr([1:1:size(ERP_shk1,1)]);
center_channels= fliplr([22:1:size(ERP_shk2,1)+21]);
right_channels= fliplr([44:1:(size(ERP_shk3,1)+43)]);
channels={left_channels';center_channels';right_channels'}';
FontName = 'MyriadPro-Regular'; % or choose any other font
FontSize = 14;
figure_width = 28;
figure_height = 14;
figuresVisible = 'on'; % 'off' for non displayed plots (will still be exported)
ERP_stacked=figure('Name','ERP_stacked');% figure('units', 'normalized', 'outerposition', [0 0 1 1]);
set(ERP_stacked, 'units', 'centimeters', 'pos', [5 5 figure_width figure_height])
set(ERP_stacked, 'PaperPositionMode', 'auto');
set(ERP_stacked, 'Color', [1 1 1]); % Sets figure background
set(ERP_stacked, 'Color', [1 1 1]); % Sets axes background
hsp = subplot(1,1,1, 'Parent', ERP_stacked);
set(hsp,'Position',[0.15 0.17 0.75 0.80]);
for ii = 1:size(channels,2)
subplot(1, size(channels,2), ii);
depth = 0;
depth_spacing = 50;
max_y = 0;
hold all;
% for each channel
chan_legends = {};
for j=1:length(channels{ii});
for i = channels{ii}(j)
if ii==1
averaged_ERP= (ERP_shk1_nonan(i,:)/(1*10^8))*(1*10^6); % scale factor for plotting(not totally sure where this came from?)
elseif ii==2
i=i-21;
averaged_ERP= (ERP_shk2_nonan(i,:)/(1*10^8))*(1*10^6);
elseif ii==3
i=i-43;
averaged_ERP= (ERP_shk3_nonan(i,:)/(1*10^8))*(1*10^6);
end
plot(X2, averaged_ERP*depth_spacing + depth,'LineWidth',2);
max_y= max_y + max(averaged_ERP);
depth= depth + depth_spacing;
chan_legends= [chan_legends, num2str(i)];
end
end
yaxis=ylim;
set(gca,'Ylim',[-depth_spacing depth_spacing*length(channels{ii})])
if ii==1
set(gca, 'ytick', [0:depth_spacing:(depth_spacing*length(channels{ii}))-depth_spacing],'tickdir','out','yticklabel',[left_channels]);
axis on; % display axis
set(gca, ...
'Box' , 'off' , ...
'TickDir' , 'out' , ...
'TickLength' , [0 0] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'XGrid' , 'off' , ...
'YGrid' , 'off' , ...
'XColor' , [.0 .0 .0], ...
'YColor' , [.0 .0 .0], ...
'LineWidth' , 0.6 );
set(gca,'Xlim',[-25 225]);
set(gca,'XTickLabel',[0 200], 'Xtick', [0 200])
xLabelText = 'Time from stimulus onset (ms)'; % greek letters in LaTeX Syntax
yLabelText = 'Electrode number (Sup-->Deep)';
% save handles to set up label properties
hXLabel = xlabel(xLabelText);
hYLabel = ylabel(yLabelText);
set([gca, hXLabel, hYLabel], ...
'FontSize' , FontSize , ...
'FontName' , FontName);
prestim_offset_y = yaxis(1):1:yaxis(2);
prestim_offset_t = ones(1, length(prestim_offset_y))*0;
plot(prestim_offset_t, prestim_offset_y, 'k', 'linewidth',2);
% poststimulus onset
elseif ii==2;
set(gca, 'ytick', [0:depth_spacing:(depth_spacing*length(channels{ii}))-depth_spacing],'tickdir','out','yticklabel',[right_channels]);
axis on; % display axis
set(gca, ...
'Box' , 'off' , ...
'TickDir' , 'out' , ...
'TickLength' , [0 0] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'XGrid' , 'off' , ...
'YGrid' , 'off' , ...
'XColor' , [.0 .0 .0], ...
'YColor' , [.0 .0 .0], ...
'LineWidth' , 0.6 );
set(gca,'Xlim',[-25 225]);
set(gca,'XTickLabel',[0 200], 'Xtick', [0 200])
xLabelText = 'Time from stimulus onset (ms)'; % greek letters in LaTeX Syntax
yLabelText = 'Electrode number (Sup-->Deep)';
% save handles to set up label properties
hXLabel = xlabel(xLabelText);
hYLabel = ylabel(yLabelText);
set([gca, hXLabel, hYLabel], ...
'FontSize' , FontSize , ...
'FontName' , FontName);
prestim_offset_y = yaxis(1):1:yaxis(2);
prestim_offset_t = ones(1, length(prestim_offset_y))*0;
plot(prestim_offset_t, prestim_offset_y, 'k', 'linewidth',2);
elseif ii==3;
set(gca, 'ytick', [0:depth_spacing:(depth_spacing*length(channels{ii}))-depth_spacing],'tickdir','out','yticklabel',[center_channels]);
axis on; % display axis
set(gca, ...
'Box' , 'off' , ...
'TickDir' , 'out' , ...
'TickLength' , [0 0] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'XGrid' , 'off' , ...
'YGrid' , 'off' , ...
'XColor' , [.0 .0 .0], ...
'YColor' , [.0 .0 .0], ...
'LineWidth' , 0.6 );
set(gca,'Xlim',[-25 225]);
set(gca,'XTickLabel',[0 200], 'Xtick', [0 200])
xLabelText = 'Time from stimulus onset (ms)'; % greek letters in LaTeX Syntax
yLabelText = 'Electrode number (Sup-->Deep)';
% save handles to set up label properties
hXLabel = xlabel(xLabelText);
hYLabel = ylabel(yLabelText);
set([gca, hXLabel, hYLabel], ...
'FontSize' , FontSize , ...
'FontName' , FontName);
prestim_offset_y = yaxis(1):1:yaxis(2);
prestim_offset_t = ones(1, length(prestim_offset_y))*0;
plot(prestim_offset_t, prestim_offset_y, 'k', 'linewidth',2);
end