| Reference | Downloads | Github

Select excel row based on button press. Using code in each frame

What are you trying to achieve?:
On each trial ‘adjust’ the image using arrow keys and then press return to select. In the example I have posted here I attempt to vary the orientation of a triangle. The values of orientation are in the excel conditions file. In my real experiment the variable will not be orientation, but will be the selected image. Each arrow press will present a different image. Therefore in my real experiment the excel file will have the filenames of images.

What did you try to make it work?:
I have been using code snippets in ‘begin experiment’ and ‘each frame’. I can successfully make the triangle change orientation when I use the ‘greened out bits of code’ and have orientation = orient.

What specifically went wrong when you tried that?:
When I try to connect the code to accessing the rows of the excel conditions file the experiment only presents the ‘begin experiment’ code orientation and stays on that one. No error message. It just doesn’t successfully execute the loop when I try to link arrow keys to variable. I can press enter as set to end the routine.
In the trial properties I have set selected row to $[RowNum]
and Conditions to an excel file with ‘angle’ defined in 6 rows.

Is it possible to select a row in the conditions file based on a button press? I have been reading various threads and saw that I shouldn’t use a ‘wait for keypress’ command in a frame script. Timing is not crucial here. As long as the change ‘feels’ smooth to user. RT isn’t important for this experiment.

Hi @kirsten, this can be achieved by extracting the list of pics from the excel file and setting the image on each button press. In you code component, add the following to the relevant tabs:

Begin Routine

picList = []
picIndex = 0
for items in trials.trialList:
    picList.append(items['pics'])  # Where 'pics' is the name of your column in Excel

Every frame

keys = event.getKeys()

if picIndex == len(picList) or picIndex == -len(picList):

if 'q' in keys:
if 'left'in keys:
elif 'right' in keys:
elif 'return' in keys:
    continueRoutine = False

End Routine

thisExp.addData('picChoice', picList[picIndex])
1 Like

Dear David,
Thank you ever so much for your time and code. I would not have thought about it that way without your help. It also helped me understand Python more.
Upon using your code I was struggling to understand what exactly to put into image properties box next to ‘image’ to link the stimulus to the code. As I was doing this, someone on another thread posted their full experiment which does exactly what I want. Lucky me! So I am now modifying that code.

So anyone interested in changing an image via button press should look here for a complete example:

Thank you again, all the best, I look forward to giving back to this helpful community in the future!