Key presses associated with colour

1 view (last 30 days)
Brett
Brett on 27 Nov 2012
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
  2 Comments
Matt Fig
Matt Fig on 28 Nov 2012
Edited: Matt Fig on 28 Nov 2012
I don't understand why your call to RANDOM does not error. The documentation says you must specify a distribution. Are you using a custom function?
Nevermind, I see you are masking the RANDOM function with a variable.... bad practice!
Brett
Brett on 30 Nov 2012
I won't happen again sir. :P

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 28 Nov 2012
Edited: Matt Fig on 28 Nov 2012
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)
  1 Comment
Brett
Brett on 30 Nov 2012
I ended up figuring it out:
if q1tri == 1 && q1col == 1 && keyCode(1,98) == 1 %down
Screen('FillRect',window,[0 0 0])
Screen('Flip',window)
WaitSecs(tar);
but thanks for the code. I hope to look more into it and get some ideas so that I can further my matlab knowledge.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 27 Nov 2012
Edited: Image Analyst on 27 Nov 2012
Your first chunk of code is unnecessarily complicated. Instead of all that looping to call randi() over and over again to get a number that hasn't been used before, just use randperm(4): assign each color to one of the elements returned from randperm(4) and be done with it.
In the second chunk of code, I'm not sure which variables represent the arrows and which represent the target colors.
  3 Comments
Image Analyst
Image Analyst on 28 Nov 2012
Good luck with that. Sorry but it would just take more time than I have to devote to it. Plus, I don't have psych toolbox.
Brett
Brett on 28 Nov 2012
No worries, thanks none-the-less. :)

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!