function [] = gui_arrows()
S.fh = figure('name','Yellow Arrow 0/0',...
'menubar','none',...
'numbert','off',...
'pos',[100 100 300 300]);
S.ax = axes('pos',[0 0 1 1],...
'visible','off',...
'handlevis','off');
S.C = {'red';'blue';'green';'yellow'};
S.C = S.C(randperm(4));
S.Px = {[30,90];[255,195];...
[150 150];[150 150]};
S.Py = {[150 150];[150 150];...
[30,90];[255,195]};
S.A(1) = annotation('arrow','units','pix',...
'X',circshift(S.Px{1},[0 rand>.5]),...
'Y',S.Py{1},...
'headstyle','plain',...
'linewidth',2,...
'color',S.C{1});
S.A(2) = annotation('arrow','units','pix',...
'X',circshift(S.Px{2},[0 rand>.5]),...
'Y',S.Py{2},...
'headstyle','plain',...
'linewidth',2,...
'color',S.C{2});
S.A(3) = annotation('arrow','units','pix',...
'X',S.Px{3},...
'Y',circshift(S.Py{3},[0 rand>.5]),...
'headstyle','plain',...
'linewidth',2,...
'color',S.C{3});
S.A(4) = annotation('arrow','units','pix',...
'X',S.Px{4},...
'Y',circshift(S.Py{4},[0 rand>.5]),...
'headstyle','plain',...
'linewidth',2,...
'color',S.C{4});
S.CNT = 0;
S.TRK = 0;
movegui('center')
set(S.fh,'windowkeypressfcn',{@fh_wkpfcn})
guidata(S.fh,S)
function [] = fh_wkpfcn(varargin)
S = guidata(gcbf);
idx = find(strcmp(S.C,'yellow'));
D1 = get(S.A(1),'X');
D2 = get(S.A(2),'X');
D3 = get(S.A(3),'Y');
D4 = get(S.A(4),'Y');
S.TRK = S.TRK + 1;
switch varargin{2}.Key
case 'uparrow'
if idx==3 && D3(1)<D3(2)
S.CNT = S.CNT + 1;
elseif idx==4 && D4(1)<D4(2)
S.CNT = S.CNT + 1;
end
case 'downarrow'
if idx==3 && D3(1)>D3(2)
S.CNT = S.CNT + 1;
elseif idx==4 && D4(1)>D4(2)
S.CNT = S.CNT + 1;
end
case 'leftarrow'
if idx==1 && D1(1)>D1(2)
S.CNT = S.CNT + 1;
elseif idx==2 && D2(1)>D2(2)
S.CNT = S.CNT + 1;
end
case 'rightarrow'
if idx==1 && D1(1)<D1(2)
S.CNT = S.CNT + 1;
elseif idx==2 && D2(1)<D2(2)
S.CNT = S.CNT + 1;
end
otherwise
end
S.C = S.C(randperm(4));
set(S.A(1),'X',circshift(S.Px{1},[0 rand>.5]),...
'color',S.C{1})
set(S.A(2),'X',circshift(S.Px{2},[0 rand>.5]),...
'color',S.C{2})
set(S.A(3),'Y',circshift(S.Py{3},[0 rand>.5]),...
'color',S.C{3})
set(S.A(4),'Y',circshift(S.Py{4},[0 rand>.5]),...
'color',S.C{4})
set(S.fh,'name',sprintf('Yellow Arrow %i/%i',S.CNT,S.TRK))
guidata(S.fh,S)
Best Answer