# Saving data from a loop in coder

hey,
i have been struggling for the past week with this problem and i’ll be very gratfull if you can help me:)
i’m doing an experiment where i need to generate diffrent matrices in a loop and the participant needs to respond according to it (i need it in a loop so that i can generate diffrent matrices each time), my proplem is that i need to run the loop three times and in the end of the exp the data saved are just from the last loop, which means that i get the data from the third time that i run the loop and the other data from the first and two times are gone (so that the number of times that i run the loop is the number of blocks in the exp [three]- and the loop is the number of steps in the block itself)
this is the code:

trials = data.TrialHandler(trialList=, nReps=3, method=‘random’,
extraInfo=expInfo, originPath=-1)
counter=0
for thisTrial in trials:# 3 blocks of full matrix
#will be 150 steps when we finish
if endExpNow or defaultKeyboard.getKeys(keyList=[“escape”]):
core.quit()
for i in range (150):
letters = [chr(i) for i in range(65, 91)]
numbers = [str(i) for i in range(10)]
polygons = [“□”,“◯”,“△”,“◇”,“⯎”,“⬠”,“⬡”]
seen_matrices = set()
MyList = [letters, numbers, polygons]
l = random.choice(MyList)
MyList.remove(l)
g = random.choice(MyList)
MyList.remove(g)
x = random.choice(MyList)
MyList.remove(x)
matrix = create_matrix(l, x, g)
matrix_tuple = tuple(matrix.flatten())
if matrix_tuple in seen_matrices:
i -= 1
continue
MyList = [letters, numbers, polygons]
print_matrix(matrix, win)
win.flip()
core.wait(0.05)
win.flip()
new_matrix = matrix.copy()
# Randomly choose the number of elements to change.
number_of_changes = np.random.choice([0, 2, 0, 4, 0, 6, 0, 8])
if number_of_changes == 0:
correctAns = ‘y’
else:
correctAns = ‘n’
for k in range(number_of_changes):
row = np.random.randint(0, 3)
column = np.random.randint(0, 3)
new_matrix[row][column] = np.random.choice(letters + numbers + polygons)
ISI = [0, 0.05, 0.1, 0.25, 0.5, 1]
if len(ISI) == 0:
ISI = [0, 0.05, 0.1, 0.25, 0.5, 1]
np.random.shuffle(ISI)
isi_duration = random.choice(ISI)
core.wait(isi_duration)
win.flip()
print_new_matrix(new_matrix, win)
win.flip()
core.wait(0.05)
win.flip()
event.waitKeys(keyList=[‘n’,‘y’])
# update component parameters for each repeat
key_resp.keys =
key_resp.rt =
key_resp_allKeys =
if key_resp.status == STARTED and not waitOnFlip:
theseKeys = key_resp.getKeys(keyList=[‘y’,‘n’], waitRelease=False)
_key_resp_allKeys.extend(theseKeys)
if len(_key_resp_allKeys):
key_resp.keys = _key_resp_allKeys[-1].name # just the last key pressed
key_resp.rt = _key_resp_allKeys[-1].rt
if (key_resp.keys == str(correctAns)) or (key_resp.keys == correctAns):
key_resp.corr = 1
else:
key_resp.corr=0
# a response ends the routine
continueRoutine = False
fix_text.draw()
win.flip()
core.wait(0.5)
win.flip()
counterLoop = counterLoop + 1
blocknum= blocknum+1
counter=counter+1
if endExpNow or defaultKeyboard.getKeys(keyList=[“escape”]):
core.quit()
# check which block
if counter<2:
# Printing the break message
break_text.draw()
win.flip()
if endExpNow or defaultKeyboard.getKeys(keyList=[“escape”]):
core.quit()
# Wait for a key press to continue
event.waitKeys(keyList=[‘space’])
again, thank you very much for your help

Which variable are you trying to store your loop information in? I want to guess new_matrix for each run, but what variable is holding the data across the all three runs?

well, i don’t want to save the new matrix, i want to save the ISI, number of elements changes and the answer- i put addData for these variables in the loop so that it saves them right away in the excel file that i made for the trial- but this doesn’t work since its saves the data on top of each other and not after it

I think you need trials.nextEntry() when you want to move to a new line in the data file.

thank you! i will try it