-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouSee.m
1154 lines (1004 loc) · 51 KB
/
MouSee.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
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
classdef MouSee < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
MouSeeUIFigure matlab.ui.Figure
StimuliPanel matlab.ui.container.Panel
RunButton matlab.ui.control.StateButton
GenerategratingsButton matlab.ui.control.StateButton
TrialLabel matlab.ui.control.Label
DirectionLabel matlab.ui.control.Label
PreviewImage matlab.ui.control.Image
TimeLabel matlab.ui.control.Label
DirectionsLabel matlab.ui.control.Label
DurationPanel matlab.ui.container.Panel
RepetitionsEditField matlab.ui.control.NumericEditField
GrayRndSpinner matlab.ui.control.Spinner
GrayEditField matlab.ui.control.NumericEditField
StimulusEditField matlab.ui.control.NumericEditField
Label matlab.ui.control.Label
RepetitionsEditFieldLabel matlab.ui.control.Label
GraysLabel matlab.ui.control.Label
StimulussLabel matlab.ui.control.Label
GratingsconfigurationPanel matlab.ui.container.Panel
OutputDropDown matlab.ui.control.DropDown
OutputvoltageDropDownLabel matlab.ui.control.Label
SinusoidalCheckBox matlab.ui.control.CheckBox
FrequencyHzEditFieldLabel matlab.ui.control.Label
GratingssizepixelsLabel matlab.ui.control.Label
RandomCheckBox matlab.ui.control.CheckBox
FrequencyHzEditField matlab.ui.control.NumericEditField
DriftingCheckBox matlab.ui.control.CheckBox
SizeEditField matlab.ui.control.NumericEditField
DirectionsDropDown matlab.ui.control.DropDown
DirectionsDropDownLabel matlab.ui.control.Label
ActivatestimulationwindowPanel matlab.ui.container.Panel
ScreenSizeLabel matlab.ui.control.Label
DefaultscreenDropDown matlab.ui.control.DropDown
DefaultscreenDropDownLabel matlab.ui.control.Label
SelectscreenLabel matlab.ui.control.Label
WindowLamp matlab.ui.control.Lamp
StimulationWindowSwitch matlab.ui.control.RockerSwitch
SkipCheckBox matlab.ui.control.CheckBox
MonitorDropDown matlab.ui.control.DropDown
end
properties (Access = public)
skip_test = 0;
screen_number = 1;
window = 0;
default_screen = 128;
static_gratings = [];
default_screen_image = [];
static_gratings_image = [];
external_texture = [];
half_width_w = 0;
half_height_w = 0;
gray = 128;
increment = 128;
real_degrees = [0 45 90 135 180 225 270 315];
num_stim = 8;
grating_pixels = 200;
stim_duration = 2;
gray_duration = 3;
gray_rnd = 0;
repetitions = 10;
cycles_per_second = 5;
rand_stim = true;
sinusoidal = true;
output_stims = false;
rotating = false;
rotation_angles = [];
workspace_stim = false;
workspace_data = [];
% textures
tex = [];
movie_frames = [];
movie_IDs = [];
image = [];
ni
cancel = false;
end
methods (Access = private)
function ComputeFinalTime(app)
total = (app.stim_duration+app.gray_duration)*app.num_stim*app.repetitions;
app.TimeLabel.Text = sprintf('Total time:\n %.1f s = %.1f min',total,total/60);
% Set properties
app.GenerategratingsButton.Text = 'Generate gratings';
app.GenerategratingsButton.BackgroundColor = [0.95 0.95 0.95];
app.RunButton.Enable = false;
end
function UpdatePreview(app)
f = app.grating_pixels/(2*pi);
angle = 0;
phase = 2*pi;
% generate grating frame
[x,y] = meshgrid(-app.half_width_w:app.half_width_w,-app.half_height_w:app.half_height_w);
a = cos(angle)/f;
b = sin(angle)/f;
% get sinusoidal or square gratings
if app.sinusoidal
m = sin(a*x+b*y+phase);
else
m = sign(sin(a*x+b*y+phase));
end
% Set properties
app.DirectionLabel.Text = 'preview';
app.PreviewImage.ImageSource = repmat(mat2gray(round(app.gray+app.increment*m)),1,1,3);
app.GenerategratingsButton.Text = 'Generate gratings';
app.GenerategratingsButton.BackgroundColor = [0.95 0.95 0.95];
app.RunButton.Enable = false;
end
function Set_Default_Screen(app)
% Set properties
app.PreviewImage.ImageSource = app.default_screen_image;
app.DirectionLabel.Text = '';
end
function GetMonitors(app)
% Get the list of screens and choose the one with the highest screen number
screens = Screen('Screens');
if ismac
n_screens = length(screens);
app.screen_number = n_screens-1;
else
n_screens = max(screens);
app.screen_number = n_screens;
end
if n_screens<=1
msgbox(['This app requires at least 2 monitors: one for stimulating ' ...
'and other for controling.'],'Error','error')
else
app.MonitorDropDown.Items = cellstr(num2str((1:n_screens)'));
app.MonitorDropDown.Value = num2str(n_screens);
end
end
function [tex,movie_frames,movie_IDs,image] = GetTextureDrifting(app,angle)
% Set the angle
angleRad = mod(180-angle,360)*pi/180;
% Run the movie animation for a fixed period
fpsScreen = Screen('FrameRate',app.screen_number);
if ~fpsScreen
fpsScreen = 60;
end
% Get number of frames and spatial frequency
nFrames = round(fpsScreen/app.FrequencyHzEditField.Value);
f = app.SizeEditField.Value/(2*pi);
% Convert movie duration in seconds to duration in frames to draw:
movie_frames = round(app.StimulusEditField.Value*fpsScreen);
movie_IDs = mod(0:(movie_frames-1),nFrames)+1;
% Compute each frame of the movie
tex = zeros(1,nFrames);
for i = 1:nFrames
phase = (i/nFrames)*2*pi;
% generate grating frame
[x,y] = meshgrid(-app.half_width_w:app.half_width_w,...
-app.half_height_w:app.half_height_w);
a = cos(angleRad)/f;
b = sin(angleRad)/f;
% get sinusoidal or square gratings
if app.sinusoidal
m = sin(a*x+b*y+phase);
else
m = sign(sin(a*x+b*y+phase));
end
if i==1
image = mat2gray(repmat(round(app.gray+app.increment*m),1,1,3));
end
tex(i) = Screen('MakeTexture',app.window,round(app.gray+app.increment*m));
end
end
function [tex,image] = GetTexture(app,angle)
% Set the angle
angleRad = mod(180-angle,360)*pi/180;
% Get spatial frequency
f = app.SizeEditField.Value/(2*pi);
% Generate grating frame
phase = 2*pi;
[x,y] = meshgrid(-app.half_width_w:app.half_width_w,...
-app.half_height_w:app.half_height_w);
a = cos(angleRad)/f;
b = sin(angleRad)/f;
% get sinusoidal or square gratings
if app.sinusoidal
m = sin(a*x+b*y+phase);
else
m = sign(sin(a*x+b*y+phase));
end
image = mat2gray(repmat(round(app.gray+app.increment*m),1,1,3));
tex = Screen('MakeTexture',app.window,round(app.gray+app.increment*m));
end
function Canceling(app)
app.RunButton.Text = 'Run';
% Default screen color
if strfind(app.DefaultscreenDropDown.Value,'%')
Screen('FillRect',app.window,app.default_screen);
elseif strfind(app.DefaultscreenDropDown.Value,'º')
Screen('DrawTexture',app.window,app.static_gratings);
else
Screen('DrawTexture',app.window,app.default_screen);
end
Screen('Flip',app.window);
if app.output_stims
app.ni.write(0);
end
app.DirectionLabel.Text = '';
app.PreviewImage.ImageSource = app.default_screen_image;
drawnow
% Enable controls
app.GratingsconfigurationPanel.Enable = 'on';
app.DurationPanel.Enable = 'on';
app.ActivatestimulationwindowPanel.Enable = 'on';
end
function [tex,movie_frames,movie_IDs,image,rotation_angles] = GetTextureRotating(app,angle,clockwise)
rotation_angle = app.FrequencyHzEditField.Value;
% Run the movie animation for a fixed period
fpsScreen = Screen('FrameRate',app.screen_number);
if ~fpsScreen
fpsScreen = 60;
end
% Get number of frames and spatial frequency
period_rotation = app.StimulusEditField.Value;
frequency_rotation = 1/period_rotation;
n_frames = round(fpsScreen/frequency_rotation);
spatial_frequency = app.SizeEditField.Value/(2*pi);
% Set initial and final angle
ini_angle_rad = angle*pi/180;
if clockwise
fin_angle_rad = (angle+rotation_angle)*pi/180;
else
fin_angle_rad = (angle-rotation_angle)*pi/180;
end
step = (fin_angle_rad-ini_angle_rad)/n_frames;
rotation_angle_rad = ini_angle_rad:step:fin_angle_rad;
rotation_angles = rotation_angle_rad*180/pi;
% Convert movie duration in seconds to duration in frames to draw:
movie_frames = round(period_rotation*fpsScreen);
movie_IDs = mod(0:(movie_frames-1),n_frames)+1;
% Compute each frame of the movie
tex = zeros(1,n_frames);
for i = 1:n_frames
% generate grating frame
[x,y] = meshgrid(-app.half_width_w:app.half_width_w,...
-app.half_height_w:app.half_height_w);
a = cos(rotation_angle_rad(i))/spatial_frequency;
b = sin(rotation_angle_rad(i))/spatial_frequency;
% get sinusoidal or square gratings
if app.sinusoidal
m = sin(a*x+b*y);
else
m = sign(sin(a*x+b*y));
end
if i==1
image = mat2gray(repmat(round(app.gray+app.increment*m),1,1,3));
end
tex(i) = Screen('MakeTexture',app.window,round(app.gray+app.increment*m));
end
end
function [tex,image] = GetWorkspaceImage(app,index)
% Set the angle
if length(size(app.workspace_data))==3
mat = repmat(app.workspace_data(:,:,index),1,1,3);
mat = imresize(mat,[app.half_height_w*2 app.half_width_w*2]);
image = mat2gray(mat);
elseif length(size(app.workspace_data))==4
mat = app.workspace_data(:,:,:,index);
mat = imresize(mat,[app.half_height_w*2 app.half_width_w*2]);
image = mat2gray(mat);
end
tex = Screen('MakeTexture',app.window,mat);
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
GetMonitors(app)
StimulationWindowSwitchValueChanged(app,[])
ComputeFinalTime(app)
if app.half_width_w
UpdatePreview(app)
end
end
% Value changed function: RunButton
function RunButtonPushed(app, event)
if app.RunButton.Value
% change text
app.RunButton.Text = 'Stop';
% Disable controls
app.GratingsconfigurationPanel.Enable = 'off';
app.DurationPanel.Enable = 'off';
app.ActivatestimulationwindowPanel.Enable = 'off';
% Use realtime priority for better timing precision:
priorityLevel = MaxPriority(app.window);
Priority(priorityLevel);
% Start the stimuli generation
app.cancel = false;
nTrials = 1;
tic
for k = 1:app.repetitions
% Do a random permutation of the stimuli
if app.rand_stim
stimOutput = randperm(app.num_stim);
else
stimOutput = 1:app.num_stim;
end
for j = 1:app.num_stim
if app.cancel
Canceling(app)
return;
end
% Get current stim
stim = stimOutput(j);
% Animation loop
delay = app.gray_duration+rand*app.gray_rnd*2-app.gray_rnd;
t = toc;
if t<delay
app.TrialLabel.Text = [' delay of ' num2str(delay,'%.1f') ' s'...
newline 'trial ' num2str(nTrials)];
drawnow
pause(delay-t)
else
app.TrialLabel.Text = ['It couldn''t achieve the time: ' ...
num2str(t-delay) 's extra'];
end
real_angle = app.real_degrees(stim);
% Plot next gratings
app.PreviewImage.ImageSource = app.image{stim};
% Rotating gratings
if app.rotating
% Plot angle
app.DirectionLabel.Text = [num2str(real_angle) '° ' app.DirectionsDropDown.Value(1)];
drawnow
% Plot movie gratings
for i = 1:app.movie_frames{stim}
if app.cancel
Canceling(app)
return;
end
% Draw image
id = app.movie_IDs{stim};
tex_i = app.tex{stim};
Screen('DrawTexture',app.window,tex_i(id(i)));
Screen('Flip',app.window);
if app.output_stims
% 0deg = 0.5V, 45deg = 1V, ..., 315deg = 4V
app.ni.write(mod(app.rotation_angles{stim}(i),360)/90+0.5);
end
end
elseif app.workspace_stim
% Static gratings
% Plot angle
app.DirectionLabel.Text = ['image ' num2str(stim)];
drawnow
% Draw static image
tex_i = app.tex{stim};
Screen('DrawTexture',app.window,tex_i);
Screen('Flip',app.window);
if app.output_stims
% 0deg = 0.5V, 45deg = 1V, ..., 315deg = 4V
app.ni.write(real_angle/90+0.5);
end
pause(app.StimulusEditField.Value)
elseif app.DriftingCheckBox.Value
% Drifitng gratings
% Plot direction and angle
app.DirectionLabel.Text = [app.DirectionsLabel.Text(stim) newline...
num2str(real_angle) '°'];
drawnow
% Plot movie gratings
for i = 1:app.movie_frames{stim}
if app.cancel
Canceling(app)
return;
end
% Draw image
id = app.movie_IDs{stim};
tex_i = app.tex{stim};
Screen('DrawTexture',app.window,tex_i(id(i)));
Screen('Flip',app.window);
if app.output_stims
% 0deg = 0.5V, 45deg = 1V, ..., 315deg = 4V
app.ni.write(real_angle/90+0.5);
end
end
else
% Static gratings
% Plot angle
app.DirectionLabel.Text = [num2str(real_angle) '°'];
drawnow
% Draw static image
tex_i = app.tex{stim};
Screen('DrawTexture',app.window,tex_i);
Screen('Flip',app.window);
if app.output_stims
% 0deg = 0.5V, 45deg = 1V, ..., 315deg = 4V
app.ni.write(real_angle/90+0.5);
end
pause(app.StimulusEditField.Value)
end
% Default screen color
if strfind(app.DefaultscreenDropDown.Value,'%')
Screen('FillRect',app.window,app.default_screen);
elseif strfind(app.DefaultscreenDropDown.Value,'º')
Screen('DrawTexture',app.window,app.static_gratings);
end
Screen('Flip',app.window);
if app.output_stims
app.ni.write(0);
end
app.DirectionLabel.Text = '';
app.PreviewImage.ImageSource = app.default_screen_image;
drawnow
nTrials = nTrials+1;
tic
end
end
app.RunButton.Text = 'Run';
app.RunButton.Value = false;
app.DirectionLabel.Text = '';
app.TrialLabel.Text = '';
% Enable controls
app.GratingsconfigurationPanel.Enable = 'on';
app.DurationPanel.Enable = 'on';
app.ActivatestimulationwindowPanel.Enable = 'on';
else
app.cancel = true;
end
end
% Value changed function: StimulationWindowSwitch
function StimulationWindowSwitchValueChanged(app, event)
value = app.StimulationWindowSwitch.Value;
if strcmp(value,'On')
% Skip test on Mac
Screen('Preference', 'SkipSyncTests', app.skip_test);
% OpenGL Psychtoolbox
AssertOpenGL;
% Get black and white color values
try
white = WhiteIndex(app.screen_number);
black = BlackIndex(app.screen_number);
catch
white = 255;
black = 0;
end
if white==1
white = 255;
end
% Make gray
app.gray = round((white+black)/2);
% Contrast 'app.increment' range for given white and gray values:
app.increment = white-app.gray;
% Open fullscreen gray window
try
[app.window,rect] = Screen('OpenWindow',app.screen_number,app.default_screen);
app.half_width_w = round(rect(3)/2);
app.half_height_w = round(rect(4)/2);
DefaultscreenDropDownValueChanged(app,[])
UpdatePreview(app)
% Change property values
app.WindowLamp.Color = [0,1,0];
app.SkipCheckBox.Enable = false;
app.MonitorDropDown.Enable = false;
app.GenerategratingsButton.Enable = true;
app.DefaultscreenDropDown.Enable = true;
app.ScreenSizeLabel.Text = ['screen size: ' num2str(rect(3)) 'x' num2str(rect(4))];
catch
% Close window:
Priority(0);
Screen('CloseAll')
% Change property values
app.WindowLamp.Color = [1,0,0];
app.StimulationWindowSwitch.Value = 'Off';
app.SkipCheckBox.Enable = true;
app.MonitorDropDown.Enable = true;
app.GenerategratingsButton.Enable = false;
app.DefaultscreenDropDown.Enable = false;
end
else
% Close window:
Priority(0);
Screen('CloseAll')
% Change property values
app.WindowLamp.Color = [0.5,0.5,0.5];
app.ScreenSizeLabel.Text = 'screen size: ----x----';
app.GenerategratingsButton.Text = 'Generate gratings';
app.GenerategratingsButton.BackgroundColor = [0.95 0.95 0.95];
app.GenerategratingsButton.Enable = false;
app.SkipCheckBox.Enable = true;
app.MonitorDropDown.Enable = true;
app.RunButton.Enable = false;
app.DefaultscreenDropDown.Enable = false;
end
end
% Value changed function: DirectionsDropDown
function DirectionsDropDownValueChanged(app, event)
value = app.DirectionsDropDown.Value;
app.DirectionsLabel.Text = value;
app.DirectionsDropDown.BackgroundColor = [0.96 0.96 0.96];
if value(1)=='↻'||value(1)=='↺'
% Enable/disable controls
app.SizeEditField.Enable = 'on';
app.DriftingCheckBox.Enable = 'off';
app.FrequencyHzEditField.Enable = 'on';
app.SinusoidalCheckBox.Enable = 'on';
% Set properties
app.FrequencyHzEditFieldLabel.Text = 'Rotation (º):';
app.FrequencyHzEditField.Value = 180;
app.rotating = true;
app.workspace_stim = false;
value = value(2:end);
elseif value(1)=='→'||value(1)=='↗'||value(1)=='↑'||value(1)=='↖'||...
value(1)=='←'||value(1)=='↙'||value(1)=='↓'||value(1)=='↘'
% Enable/disable controls
app.SizeEditField.Enable = 'on';
app.DriftingCheckBox.Enable = 'on';
app.SinusoidalCheckBox.Enable = 'on';
if ~app.DriftingCheckBox.Value
app.FrequencyHzEditField.Enable = 'off';
end
% Set properties
app.FrequencyHzEditFieldLabel.Text = 'Frequency (Hz):';
app.FrequencyHzEditField.Value = 2;
app.rotating = false;
app.workspace_stim = false;
else
% Enable/disable controls
app.SizeEditField.Enable = 'off';
app.DriftingCheckBox.Enable = 'off';
app.FrequencyHzEditField.Enable = 'off';
app.SinusoidalCheckBox.Enable = 'off';
% Set properties
app.rotating = false;
app.workspace_stim = true;
end
if app.workspace_stim
% Check if variable exist
if evalin('base',['exist(''' value ''',''var'')'])
app.workspace_data = evalin('base',value);
if length(size(app.workspace_data))==3
app.num_stim = size(app.workspace_data,3);
elseif length(size(app.workspace_data))==4
app.num_stim = size(app.workspace_data,4);
end
app.real_degrees = (1:app.num_stim)*360/app.num_stim;
app.DirectionsDropDown.BackgroundColor = [0 1 0];
else
app.num_stim = 0;
app.DirectionsDropDown.BackgroundColor = [1 0 0];
end
else
real_deg = [0 45 90 135 180 225 270 315];
arrows = '→↗↑↖←↙↓↘';
app.real_degrees = [];
for i = 1:8
if contains(value,arrows(i))
app.real_degrees(end+1) = real_deg(i);
end
end
app.num_stim = length(app.real_degrees);
end
ComputeFinalTime(app)
end
% Value changed function: SizeEditField
function SizeEditFieldValueChanged(app, event)
app.grating_pixels = app.SizeEditField.Value;
DefaultscreenDropDownValueChanged(app,event)
UpdatePreview(app)
end
% Value changed function: FrequencyHzEditField
function FrequencyHzEditFieldValueChanged(app, event)
app.cycles_per_second = app.FrequencyHzEditField.Value;
UpdatePreview(app)
end
% Value changed function: RandomCheckBox
function RandomCheckBoxValueChanged(app, event)
app.rand_stim = app.RandomCheckBox.Value;
end
% Value changed function: SinusoidalCheckBox
function SinusoidalCheckBoxValueChanged(app, event)
app.sinusoidal = app.SinusoidalCheckBox.Value;
UpdatePreview(app)
end
% Value changed function: SkipCheckBox
function SkipCheckBoxValueChanged(app, event)
if app.SkipCheckBox.Value
app.skip_test = 1;
app.SkipCheckBox.FontColor = [1 0 0];
else
app.skip_test = 0;
app.SkipCheckBox.FontColor = [0 0 0];
end
end
% Value changed function: OutputDropDown
function OutputDropDownValueChanged(app, event)
value = app.OutputDropDown.Value;
if strcmp(value,'None')
app.output_stims = false;
app.OutputDropDown.BackgroundColor = [0.96 0.96 0.96];
else
app.output_stims = true;
device = value(1:4);
channel = value(8:10);
try
app.ni = daq('ni');
app.ni.addoutput(device,channel,'Voltage');
app.ni.IsContinuous = true;
app.ni.write(0);
app.OutputDropDown.BackgroundColor = [0 1 0];
catch me
app.OutputDropDown.BackgroundColor = [1 1 0];
msgbox(me.identifier,"Warning","warn","modal")
end
end
end
% Value changed function: StimulusEditField
function StimulusEditFieldValueChanged(app, event)
app.stim_duration = app.StimulusEditField.Value;
ComputeFinalTime(app)
end
% Value changed function: GrayEditField
function GrayEditFieldValueChanged(app, event)
app.gray_duration = app.GrayEditField.Value;
if app.gray_duration>0
app.GrayRndSpinner.Limits = [0 app.gray_duration];
app.GrayRndSpinner.Enable = true;
else
app.GrayRndSpinner.Limits = [0 inf];
app.GrayRndSpinner.Enable = false;
end
ComputeFinalTime(app)
end
% Value changed function: RepetitionsEditField
function RepetitionsEditFieldValueChanged(app, event)
app.repetitions = app.RepetitionsEditField.Value;
ComputeFinalTime(app)
end
% Close request function: MouSeeUIFigure
function MouSeeUIFigureCloseRequest(app, event)
try
Priority(0);
Screen('CloseAll')
delete(app)
catch
delete(app)
end
end
% Value changed function: MonitorDropDown
function MonitorDropDownValueChanged(app, event)
if ismac
app.screen_number = str2double(app.MonitorDropDown.Value)-1;
else
app.screen_number = str2double(app.MonitorDropDown.Value);
end
end
% Value changed function: GrayRndSpinner
function GrayRndSpinnerValueChanged(app, event)
app.gray_rnd = app.GrayRndSpinner.Value;
end
% Value changed function: DriftingCheckBox
function DriftingCheckBoxValueChanged(app, event)
if app.DriftingCheckBox.Value
app.FrequencyHzEditField.Enable = true;
else
app.FrequencyHzEditField.Enable = false;
end
UpdatePreview(app)
end
% Value changed function: GenerategratingsButton
function GenerategratingsButtonValueChanged(app, event)
value = app.GenerategratingsButton.Value;
if value
for i = 1:app.num_stim
% Rotating gratings
if app.workspace_stim
[app.tex{i},app.image{i}] = GetWorkspaceImage(app,i);
elseif app.DirectionsDropDown.Value(1)=='↺'
[app.tex{i},app.movie_frames{i},app.movie_IDs{i},app.image{i},...
app.rotation_angles{i}] = ...
GetTextureRotating(app,app.real_degrees(i),false);
elseif app.DirectionsDropDown.Value(1)=='↻'
[app.tex{i},app.movie_frames{i},app.movie_IDs{i},app.image{i},...
app.rotation_angles{i}] = ...
GetTextureRotating(app,app.real_degrees(i),true);
elseif app.DriftingCheckBox.Value
[app.tex{i},app.movie_frames{i},app.movie_IDs{i},app.image{i}] = ...
GetTextureDrifting(app,app.real_degrees(i));
else
[app.tex{i},app.image{i}] = GetTexture(app,app.real_degrees(i));
end
end
app.RunButton.Enable = true;
app.GenerategratingsButton.BackgroundColor = [0 1 0];
app.GenerategratingsButton.Value = false;
app.GenerategratingsButton.Text = 'Gratings generated!';
end
end
% Value changed function: DefaultscreenDropDown
function DefaultscreenDropDownValueChanged(app, event)
value = app.DefaultscreenDropDown.Value;
app.DefaultscreenDropDown.BackgroundColor = [0.96 0.96 0.96];
switch value
case {'0% - black','10% - gray','20% - gray','30% - gray','40% - gray',...
'50% - gray','60% - gray','70% - gray','80% - gray','90% - gray',...
'100% - white'}
% Grayscale
id = strfind(value,'%');
color = str2double(value(1:id-1));
app.default_screen = round(color/100*255)+1;
[~,im] = GetTexture(app,0);
app.default_screen_image = uint8(app.default_screen*ones(size(im)));
Screen('FillRect',app.window,app.default_screen);
Screen('Flip',app.window);
case {'0º - gratings','45º - gratings','90º - gratings','135º - gratings'}
% Gratings
id = strfind(value,'º');
angle = str2double(value(1:id-1));
[app.static_gratings,app.default_screen_image] = GetTexture(app,angle);
Screen('DrawTexture',app.window,app.static_gratings);
Screen('Flip',app.window);
case '...'
% Import image
[file_name,path] = uigetfile('*.png;*.jpg;*.jpeg;*.tiff','Select an image');
if file_name
file = [path file_name];
im = imread(file);
app.external_texture = Screen('MakeTexture',app.window,uint8(im));
else
app.default_screen = 128;
app.DefaultscreenDropDown.Value = 6;
end
otherwise
if evalin('base',['exist(''' value ''',''var'')'])
app.default_screen = evalin('base',value);
if length(size(app.default_screen))==2
app.default_screen = repmat(app.default_screen,1,1,3);
end
Screen('DrawTexture',app.window,app.default_screen);
Screen('Flip',app.window);
app.DefaultscreenDropDown.BackgroundColor = [0 1 0];
else
app.DefaultscreenDropDown.BackgroundColor = [1 0 0];
end
end
Set_Default_Screen(app)
end
% Drop down opening function: DirectionsDropDown
function DirectionsDropDownOpening(app, event)
basic_items = {'→↗↑↖←↙↓↘','→↗↑↖','←↙↓↘','→↑←↓','→↑','←↓','↗↖','↙↘',...
'→','↗','↑','↖','←','↙','↓','↘',...
'↻→↗↑↖','↻→↑','↻↗↖','↻→','↻↗','↻↑','↻↖',...
'↺→↗↑↖','↺→↑','↺↗↖','↺→','↺↗','↺↑','↺↖'};
data_strings = evalin('base','who');
app.DirectionsDropDown.Items = [basic_items data_strings'];
end
% Drop down opening function: DefaultscreenDropDown
function DefaultscreenDropDownOpening(app, event)
basic_items = {'0% - black','10% - gray','20% - gray','30% - gray','40% - gray',...
'50% - gray','60% - gray','70% - gray','80% - gray','90% - gray',...
'100% - white','0º - gratings','45º - gratings','90º - gratings',...
'135º - gratings'};
data_strings = evalin('base','who');
app.DirectionsDropDown.Items = [basic_items data_strings'];
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create MouSeeUIFigure and hide until all components are created
app.MouSeeUIFigure = uifigure('Visible', 'off');
app.MouSeeUIFigure.Position = [100 100 510 549];
app.MouSeeUIFigure.Name = 'MouSee';
app.MouSeeUIFigure.CloseRequestFcn = createCallbackFcn(app, @MouSeeUIFigureCloseRequest, true);
% Create ActivatestimulationwindowPanel
app.ActivatestimulationwindowPanel = uipanel(app.MouSeeUIFigure);
app.ActivatestimulationwindowPanel.AutoResizeChildren = 'off';
app.ActivatestimulationwindowPanel.Title = 'Activate stimulation window';
app.ActivatestimulationwindowPanel.Position = [8 365 226 170];
% Create MonitorDropDown
app.MonitorDropDown = uidropdown(app.ActivatestimulationwindowPanel);
app.MonitorDropDown.Items = {'1'};
app.MonitorDropDown.ValueChangedFcn = createCallbackFcn(app, @MonitorDropDownValueChanged, true);
app.MonitorDropDown.Position = [103 120 101 22];
app.MonitorDropDown.Value = '1';
% Create SkipCheckBox
app.SkipCheckBox = uicheckbox(app.ActivatestimulationwindowPanel);
app.SkipCheckBox.ValueChangedFcn = createCallbackFcn(app, @SkipCheckBoxValueChanged, true);
app.SkipCheckBox.Tooltip = {'Windows users: If skip is selected this will seriously impair proper stimulus presentation and stimulus presentation timing!'; 'Mac users: must be skip in order to run the code.'};
app.SkipCheckBox.Text = 'Skip synchronization test ⚠';
app.SkipCheckBox.Position = [9 91 172 22];
% Create StimulationWindowSwitch
app.StimulationWindowSwitch = uiswitch(app.ActivatestimulationwindowPanel, 'rocker');
app.StimulationWindowSwitch.Orientation = 'horizontal';
app.StimulationWindowSwitch.ValueChangedFcn = createCallbackFcn(app, @StimulationWindowSwitchValueChanged, true);
app.StimulationWindowSwitch.Position = [51 62 45 20];
% Create WindowLamp
app.WindowLamp = uilamp(app.ActivatestimulationwindowPanel);
app.WindowLamp.Position = [154 39 45 45];
app.WindowLamp.Color = [0.651 0.651 0.651];
% Create SelectscreenLabel
app.SelectscreenLabel = uilabel(app.ActivatestimulationwindowPanel);
app.SelectscreenLabel.Position = [8 120 82 22];
app.SelectscreenLabel.Text = 'Select screen:';
% Create DefaultscreenDropDownLabel
app.DefaultscreenDropDownLabel = uilabel(app.ActivatestimulationwindowPanel);
app.DefaultscreenDropDownLabel.Enable = 'off';
app.DefaultscreenDropDownLabel.Position = [8 10 86 22];
app.DefaultscreenDropDownLabel.Text = 'Default screen:';
% Create DefaultscreenDropDown
app.DefaultscreenDropDown = uidropdown(app.ActivatestimulationwindowPanel);
app.DefaultscreenDropDown.Items = {'0% - black', '10% - gray', '20% - gray', '30% - gray', '40% - gray', '50% - gray', '60% - gray', '70% - gray', '80% - gray', '90% - gray', '100% - white', '0º - gratings', '45º - gratings', '90º - gratings', '135º - gratings'};
app.DefaultscreenDropDown.DropDownOpeningFcn = createCallbackFcn(app, @DefaultscreenDropDownOpening, true);
app.DefaultscreenDropDown.ValueChangedFcn = createCallbackFcn(app, @DefaultscreenDropDownValueChanged, true);
app.DefaultscreenDropDown.Enable = 'off';
app.DefaultscreenDropDown.Position = [103 10 104 22];
app.DefaultscreenDropDown.Value = '50% - gray';
% Create ScreenSizeLabel
app.ScreenSizeLabel = uilabel(app.ActivatestimulationwindowPanel);
app.ScreenSizeLabel.HorizontalAlignment = 'center';
app.ScreenSizeLabel.FontColor = [0.651 0.651 0.651];
app.ScreenSizeLabel.Position = [2 38 143 22];
app.ScreenSizeLabel.Text = 'screen size: ----x----';
% Create GratingsconfigurationPanel
app.GratingsconfigurationPanel = uipanel(app.MouSeeUIFigure);
app.GratingsconfigurationPanel.AutoResizeChildren = 'off';
app.GratingsconfigurationPanel.Title = 'Gratings configuration';
app.GratingsconfigurationPanel.Position = [8 137 225 220];
% Create DirectionsDropDownLabel
app.DirectionsDropDownLabel = uilabel(app.GratingsconfigurationPanel);
app.DirectionsDropDownLabel.Position = [7 167 63 22];
app.DirectionsDropDownLabel.Text = 'Directions:';
% Create DirectionsDropDown
app.DirectionsDropDown = uidropdown(app.GratingsconfigurationPanel);
app.DirectionsDropDown.Items = {'→↗↑↖←↙↓↘', '→↗↑↖', '←↙↓↘', '→↑←↓', '→↑', '←↓', '↗↖', '↙↘', '→', '↗', '↑', '↖', '←', '↙', '↓', '↘', '↻→↗↑↖', '↻→↑', '↻↗↖', '↻→', '↻↗', '↻↑', '↻↖', '↺→↗↑↖', '↺→↑', '↺↗↖', '↺→', '↺↗', '↺↑', '↺↖'};
app.DirectionsDropDown.DropDownOpeningFcn = createCallbackFcn(app, @DirectionsDropDownOpening, true);
app.DirectionsDropDown.ValueChangedFcn = createCallbackFcn(app, @DirectionsDropDownValueChanged, true);
app.DirectionsDropDown.Position = [77 167 129 22];
app.DirectionsDropDown.Value = '→↗↑↖←↙↓↘';
% Create SizeEditField
app.SizeEditField = uieditfield(app.GratingsconfigurationPanel, 'numeric');
app.SizeEditField.Limits = [1 4000];
app.SizeEditField.RoundFractionalValues = 'on';
app.SizeEditField.ValueChangedFcn = createCallbackFcn(app, @SizeEditFieldValueChanged, true);
app.SizeEditField.Position = [133 140 73 22];
app.SizeEditField.Value = 200;
% Create DriftingCheckBox
app.DriftingCheckBox = uicheckbox(app.GratingsconfigurationPanel);
app.DriftingCheckBox.ValueChangedFcn = createCallbackFcn(app, @DriftingCheckBoxValueChanged, true);
app.DriftingCheckBox.Text = 'Drifting';
app.DriftingCheckBox.Position = [7 113 78 22];
app.DriftingCheckBox.Value = true;
% Create FrequencyHzEditField
app.FrequencyHzEditField = uieditfield(app.GratingsconfigurationPanel, 'numeric');
app.FrequencyHzEditField.Limits = [0.001 Inf];
app.FrequencyHzEditField.ValueChangedFcn = createCallbackFcn(app, @FrequencyHzEditFieldValueChanged, true);
app.FrequencyHzEditField.Position = [133 86 73 22];
app.FrequencyHzEditField.Value = 2;
% Create RandomCheckBox
app.RandomCheckBox = uicheckbox(app.GratingsconfigurationPanel);
app.RandomCheckBox.ValueChangedFcn = createCallbackFcn(app, @RandomCheckBoxValueChanged, true);
app.RandomCheckBox.Text = 'Random sequence';
app.RandomCheckBox.Position = [7 33 189 22];
app.RandomCheckBox.Value = true;
% Create GratingssizepixelsLabel
app.GratingssizepixelsLabel = uilabel(app.GratingsconfigurationPanel);
app.GratingssizepixelsLabel.Position = [7 140 119 22];
app.GratingssizepixelsLabel.Text = 'Gratings size (pixels):';
% Create FrequencyHzEditFieldLabel
app.FrequencyHzEditFieldLabel = uilabel(app.GratingsconfigurationPanel);
app.FrequencyHzEditFieldLabel.Position = [23 86 103 22];
app.FrequencyHzEditFieldLabel.Text = 'Frequency (Hz):';
% Create SinusoidalCheckBox
app.SinusoidalCheckBox = uicheckbox(app.GratingsconfigurationPanel);
app.SinusoidalCheckBox.ValueChangedFcn = createCallbackFcn(app, @SinusoidalCheckBoxValueChanged, true);
app.SinusoidalCheckBox.Text = 'Sinusoidal';
app.SinusoidalCheckBox.Position = [7 59 78 22];
app.SinusoidalCheckBox.Value = true;
% Create OutputvoltageDropDownLabel
app.OutputvoltageDropDownLabel = uilabel(app.GratingsconfigurationPanel);
app.OutputvoltageDropDownLabel.Position = [8 7 88 22];