PDA

View Full Version : Writing data to multiple files


katewilmut
06-08-2009, 03:03 AM
Hi,

I am only a starter with Vizard so this is probably a basic question.

I am using a flock of birds to collect position and orientation data. I have set up a program so it collects data for a set amount of time every time I press a key. I want to write each of these 'pieces' of data to a different excel file (trial 1 in 1.xls, trial 2 in 2.xls etc). In Matlab I would use a for loop to do this, which is want I have tried, but I can't get it to work in Vizard.

The loop doesn't seem to iterate each time, and I can't see a way to tell it to.

Many thanks

Kate

Jeff
06-08-2009, 02:00 PM
There is a link to python documentation in Vizard under the help tab. There are some examples of using a for loop in there. If you are still having trouble try posting an example of what's not working.

katewilmut
06-09-2009, 12:54 AM
Jeff,

I have tried using the help and could get any further - below is the code I am using. At I said before the loop at the bottom just gets stuck and does iterate onwards for each button press.

Any help is greatly appreciated

Kate


import viz
import math
import viztask
import random
import vizjoy

viz.go()

subject = viz.input('What is the participant number?')

viz.move(0,0,0)
viz.eyeheight(0)

Trial = 0
myTracker = viz.add('flockofbirds.dls')
myTracker.reset()
viz.tracker()
ball=viz.add('soccerball.ive')
ballinitial = [0,0,8]
ball.setPosition(ballinitial)

def onKeyDown (key):
viz.callback( viz.TIMER_EVENT, mytimer )
viz.starttimer(0, 1/100, 5)

def mytimer(num):
print Trial
data = myTracker.get()
print data
a = data[0]
b = data[1]
c = data[2]
d = data[3]
e = data[4]
f = data[5]
tracking_data = open('tracking_'+ str(subject) + '_' + str(Trial) + '.xls', 'a')
tracking_data.write(str(a) + ' ' + str(b) + ' ' + str(c) + ' ' + str(d) + ' ' + str(e) + ' ' + str(f) + '\n')

for i in range (0,9):
global Trial
Trial = i
viz.callback(viz.KEYDOWN_EVENT, onKeyDown)

katewilmut
06-09-2009, 04:44 AM
Jeff,

I have tried using the help and could get any further - below is the code I am using. At I said before the loop at the bottom just gets stuck and does iterate onwards for each button press.

Any help is greatly appreciated

Kate

import viz
import math
import viztask
import random
import vizjoy

viz.go()

subject = viz.input('What is the participant number?')

viz.move(0,0,0)
viz.eyeheight(0)

Trial = 0
myTracker = viz.add('flockofbirds.dls')
myTracker.reset()
viz.tracker()
ball=viz.add('soccerball.ive')
ballinitial = [0,0,8]
ball.setPosition(ballinitial)

def onKeyDown (key):
viz.callback( viz.TIMER_EVENT, mytimer )
viz.starttimer(0, 1/100, 5)

def mytimer(num):
print Trial
data = myTracker.get()
print data
a = data[0]
b = data[1]
c = data[2]
d = data[3]
e = data[4]
f = data[5]
tracking_data = open('tracking_'+ str(subject) + '_' + str(Trial) + '.xls', 'a')
tracking_data.write(str(a) + ' ' + str(b) + ' ' + str(c) + ' ' + str(d) + ' ' + str(e) + ' ' + str(f) + '\n')

for i in range (0,9):
global Trial
Trial = i
viz.callback(viz.KEYDOWN_EVENT, onKeyDown)

Jeff
06-10-2009, 12:31 PM
You loop should work, but there is no connection between it and your callbacks. It loops through and then leaves Trial set to a value of 8. If you want Trial to update each time a key is pressed then increment it in your keyboard callback function.
Trial = 0
def onKeyDown(key):
global Trial
Trial += 1

viz.callback(viz.KEYDOWN_EVENT, onKeyDown)

katewilmut
06-11-2009, 01:03 AM
Thank you so much, I knew it was something silly I was missing but I just couldn't work it out. Much appreciated

Kate