# Saving data from a loop in coder

**URL:** <https://discourse.psychopy.org/t/saving-data-from-a-loop-in-coder/36987>\
**Category:** Coding\
**Created:** [October 21, 2023, 9:43pm UTC](https://discourse.psychopy.org/t/saving-data-from-a-loop-in-coder/36987 "2023-10-21T21:43:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mosana](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mosana](https://discourse.psychopy.org/u/mosana)\
**Post date:** [October 21, 2023, 9:43pm UTC](https://discourse.psychopy.org/t/saving-data-from-a-loop-in-coder/36987/1 "2023-10-21T21:43:34Z")

</div>

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  
seen\_matrices.add(matrix\_tuple)  
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])  
trials.addData(‘number\_of\_changes’, number\_of\_changes)  
if number\_of\_changes == 0:  
correctAns = ‘y’  
trials.addData(‘correctAns’, correctAns)  
else:  
correctAns = ‘n’  
trials.addData(‘correctAns’, correctAns)  
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)  
trials.addData(‘ISI’, isi\_duration)  
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  
trials.addData(‘respAns’, key\_resp.corr)  
fix\_text.draw()  
win.flip()  
core.wait(0.5)  
win.flip()  
counterLoop = counterLoop + 1  
trials.addData(‘loop number’, counterLoop)  
trials.addData(‘block number’, blocknum)  
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

---

<div class="post-metadata">

**Author:** ![Antknei](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Antknei](https://discourse.psychopy.org/u/Antknei)\
**Post date:** [October 22, 2023, 11:42pm UTC](https://discourse.psychopy.org/t/saving-data-from-a-loop-in-coder/36987/2 "2023-10-22T23:42:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mosana](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mosana](https://discourse.psychopy.org/u/mosana)\
**Post date:** [October 26, 2023, 3:28pm UTC](https://discourse.psychopy.org/t/saving-data-from-a-loop-in-coder/36987/3 "2023-10-26T15:28:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [October 26, 2023, 4:25pm UTC](https://discourse.psychopy.org/t/saving-data-from-a-loop-in-coder/36987/4 "2023-10-26T16:25:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mosana](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mosana](https://discourse.psychopy.org/u/mosana)\
**Post date:** [October 26, 2023, 4:44pm UTC](https://discourse.psychopy.org/t/saving-data-from-a-loop-in-coder/36987/5 "2023-10-26T16:44:12Z")

</div>

thank you! i will try it
