Key presses associated with colour

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!
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

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

Brett
Brett on 28 Nov 2012
Edited: Brett on 28 Nov 2012
New code for color:
random5 = random(1)
random6 = random(2)
random7 = random(3)
random9 = random(4)
if random5 == 1
colour1 = c1
elseif random5 == 2
colour1 = c2
elseif random5 == 3
colour1 = c3
else
colour1 = c4
end
if random6 == 1
colour2 = c1
elseif random6 == 2
colour2 = c2
elseif random6 == 3
colour2 = c3
else
colour2 = c4
end
if random7 == 1
colour3 = c1
elseif random7 == 2
colour3 = c2
elseif random7 == 3
colour3 = c3
else
colour3 = c4
end
if random8 == 1
colour4 = c1
elseif random8 == 2
colour4 = c2
elseif random8 == 3
colour4 = c3
else
colour4 = c4
end
The second set of code only represents the arrows.
tri2 = downtri
b2 = vertbaseq2
The above example represents a downward facing arrow. The way the program works it is changes the color of a oval at a particular location:
disc4 = [X-(-170),Y-330,X+330,Y+(-170)]
Screen('FillOval',window,colour4, disc4)
I then have arrows are displayed on top of the ovals:
rx = 890;
ty = 262;
q2 = tri2 + repmat([rx, ty], 3, 1)
q2 and disc4 are roughly the same location. I want the arrow keys to be associated with the arrow displayed, and if the arrow key presses is the same as arrow key inside the target color (c1) it will result in the next screen to appear for a shorter period of time. What I think I need is code that says something like if colour1 = c1, and any arrow at coordinates (lx,ty) will have shorter wait time than anything else.
I started to write the code for this but 1) I don't know how to factor in the coordinates (lx,ty part) and 2) I get error: "The expression to the left of the equals sign is not a valid target for an assignment" at the if colour1 = c1 line.
[keyIsDown,secs,keyCode, deltaSecs] = KbCheck
while keyIsDown == 0; %Will present this screen until a key is pressed
[keyIsDown, secs, keyCode, deltaSecs] = KbCheck;
%RT = GetSecs - starttrial - time;
if colour1 = c1
if (keyCode(1,100)) == 1; %left
Screen('FillRect',window,[0 0 0])
Screen('Flip',window)
WaitSecs(tar);
elseif keyCode(1,104) == 1; %up
Screen('FillRect',window,[0 0 0])
Screen('Flip',window)
WaitSecs(tar);
elseif keyCode(1,102) == 1; %right
Screen('FillRect',window,[0 0 0])
Screen('Flip',window)
WaitSecs(.tar);
elseif keyCode(1,98) == 1; %down
Screen('FillRect',window,[0 0 0])
Screen('Flip',window)
WaitSecs(tar);
end
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.
No worries, thanks none-the-less. :)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 27 Nov 2012

Community Treasure Hunt

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

Start Hunting!