MATLAB: Key presses associated with colour

colorkey presstarget with random

This is a tricky one to explain so bare with me. I have four colors that randomize so no color is the same as any other color:
colarray = [c1; c2; c3; c4];
colidx1 = randi(4,1,1);
colour1 = colarray(colidx1,:);
colidx2 = randi(4,1,1);
while ismember(colidx2, colidx1)
colidx2 = randi(4,1,1);
end
colour2 = colarray(colidx2,:);
colidx3 = randi(4,1,1);
while ismember(colidx3, [colidx1, colidx2])
colidx3 = randi(4,1,1);
end
colour3 = colarray(colidx3,:);
colidx4 = randi(4,1,1);
while ismember(colidx4, [colidx1, colidx2, colidx3])
colidx4 = randi (4,1,1);
end
colour4 = colarray(colidx4,:);
and I have four arrows that randomize so that no arrow is facing the same direction as any other arrow:
random = randperm(4)
random1 = random(1)
random2 = random(2)
random3 = random(3)
random4 = random(4)
if random1 == 1
tri1 = downtri
b1 = vertbaseq1
elseif random1 == 2
tri1 = uptri
b1 = vertbaseq1
elseif random1 == 3
tri1 = righttri
b1 = horzbaseq1
else
tri1 = lefttri
b1 = horzbaseq1
end
if random2 == 1
tri3 = downtri
b3 = vertbaseq3
elseif random2 == 2
tri3 = uptri
b3 = vertbaseq3
elseif random2 == 3
tri3 = righttri
b3 = horzbaseq3
else
tri3 = lefttri
b3 = horzbaseq3
end
if random3 == 1
tri4 = downtri
b4 = vertbaseq4
elseif random3 == 2
tri4 = uptri
b4 = vertbaseq4
elseif random3 == 3
tri4 = righttri
b4 = horzbaseq4
else
tri4 = lefttri
b4 = horzbaseq4
end
if random4 == 1
tri2 = downtri
b2 = vertbaseq2
elseif random4 == 2
tri2 = uptri
b2 = vertbaseq2
elseif random4 == 3
tri2 = righttri
b2 = horzbaseq2
else
tri2 = lefttri
b2 = horzbaseq2
end
Each arrow will correspond to the arrow keys on the keyboard and the color will appear in an oval behind the arrow. I have a target color (c1). I want some way of saying if the arrow is the target color, then there will be a different response than if the person selects an arrow that isn't the target color. I'm not too worried about the keypresses (I think I can figure that out), but I'm not sure how to associated arrow direction with the target color.
Any help will be greatly appreciated, Brett

Best Answer

I have no idea what you are doing by looking at the code as there is too much left out. But from your description, this might give you some ideas. The user is supposed to hit the arrow that is yellow. The count of successes is in the figure title. It's pretty simple, but it was fun to make.
function [] = gui_arrows()
% Associate arrows with keypress...
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) % Store the structure.
function [] = fh_wkpfcn(varargin)
% Callback for figure windowbuttondownfcn
S = guidata(gcbf); % Get the structure.
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)