PDA

View Full Version : text output


jaclyn.bill
10-23-2007, 02:17 AM
Dear all,

I'm using the script below in order to move a ball around the screen and save data to two text files. The first records keyboard and time responses, the second records x,y,z of the ball across time.

Whilst the keyboard responses are recording to text fine and the x,y,z data is visible in the output screen of vizard, only the last x,y,z co-ord is saving to the text file. Is there a simple command I'm missing, or is the issue because I'm trying to write two files at once?

I would be grateful for some help on this.

Thank you.

Jac

####code for recording the data.
def RecordTimer(pos, currentTime):
file = open ('time.txt','w')
currentTime = viz.tick()
pos = gball.getPosition()
save = '%d %.4f %.4f %.4f'%(currentTime * 1000,pos[0],pos[1],pos[2])###original
file.write(save)
file.flush()
print save

def RunSimulation():

#Repeat simulation indefinitely
while True:

#Wait for left mouse button to be pressed
yield viztask.waitMouseDown(viz.MOUSEBUTTON_LEFT)

#Place ball in starting position
gball.add(motionseq)

#Start timer to record ball position
recordTimer = vizact.onupdate(0,RecordTimer)

#Wait for ball to finish move action
yield viztask.runAction(gball.add(motionseq))

#Stop timer
vizact.removeEvent(recordTimer)

viztask.schedule(RunSimulation())


import vizinfo

# Opens file 'response.txt' in write mode
file = open('response.txt', 'w')

# Define a function that saves data
def SaveData(currenttime, key):
out = str(currenttime) + '\t' + key + '\n'
file.write(out)
file.flush()
print out

# Define a function that is called every time a keyboard button is pressed
def mykeyboard(key):
# Calls the function SaveData and hands it the current time and key
SaveData(viz.tick(),key)

# Defines a callback for keyboard events
viz.callback(viz.KEYBOARD_EVENT, mykeyboard)

farshizzo
10-23-2007, 10:46 AM
You are opening the file during every iteration of your timer, this is very inefficient. Also, whenever you open a file in write mode (i.e. 'w') it will overwrite the current contents of the file. What you should do is open the file once, and just write to it inside your timer. Example:logFile = open ('time.txt','w')
def RecordTimer(pos, currentTime):
currentTime = viz.tick()
pos = gball.getPosition()
save = '%d %.4f %.4f %.4f'%(currentTime * 1000,pos[0],pos[1],pos[2])###original
logFile.write(save)
print saveAlso, if you want to append data to a file, you can open it using the 'a' flag, instead of the 'w' flag.

jaclyn.bill
10-24-2007, 06:37 AM
That's great, thanks for your help.

Jac