Page 1 of 1
Writing contents of vector to a file?
Posted: Wed Aug 14, 2013 12:29 pm
by oregoneuron
I am trying to record the results of simulations run using the GUI to a vector and then write that vector to a text file. When I open the hoc file, a text file with the correct name is created, but, after running simulations with the GUI, the text file still contains no data. Here is the code for the creation of the vector and the creation of the file. These come after the specification of the biophysics of the model:
Code: Select all
// New vector for recording Vsoma
objref vvec, tvec
tvec = new Vector()
tvec.record(&t) // record time
vvec = new Vector()
soma vvec.record(&v(0.5)) // record v at soma
// New file for recording vector
objref f1
f1 = new File()
f1.wopen("grcv.txt")
tvec.printf(f1)
vvec.printf(f1)
f1.close()
I have a custom initialization procedure after this code section that injects a constant current to set the resting membrane potential. What is going on here and how do I fix it?
Thanks in advance.
Re: Writing contents of vector to a file?
Posted: Wed Aug 14, 2013 3:10 pm
by ted
What is going on here and how do I fix it?
The sequence of events is important. Walk through the code yourself and pretend you're the computer. Remember, the computer executes code serially, i.e. if the sequence of instructions is
A
B
C
D
then A is performed first, next B, then C, and finally D. You need to run a simulation before you write the vectors to the file.
Re: Writing contents of vector to a file?
Posted: Wed Aug 14, 2013 5:18 pm
by oregoneuron
Hi Ted. Thanks for the rapid reply. I changed my code to have the write-to-file portion after the simulation run controls. Here's the code:
Code: Select all
// New vector for recording Vsoma
objref vvec, tvec
tvec = new Vector()
tvec.record(&t) // record time
vvec = new Vector()
soma vvec.record(&v(0.5)) // record v at soma
// Simulation control
dt = 0.025
tstop = 550
v_init = -65
proc init() {
finitialize(-70)
ic_constant = -(ina + ik + il_GrC_LKG1)
if (cvode.active()) {
cvode.re_init()
} else {
fcurrent()
}
frecord_init()
}
proc integrate() {
while (t<tstop) {
fadvance()
}
}
proc go() {
init()
integrate()
}
// New file for recording vector
objref f1
f1 = new File()
f1.wopen("grcv.txt")
tvec.printf(f1)
vvec.printf(f1)
f1.close()
However, when I open the hoc file with Neuron and type go() I still get an empty text file. Where is a more appropriate place to put the file creation code?
Re: Writing contents of vector to a file?
Posted: Wed Aug 14, 2013 9:16 pm
by ted
You've been looking at the code so long it's easy to miss things; that's how you forgot to execute proc go() before writing the vectors to a file. Just insert the statement
go()
before the file-related code.
BTW there's no need for proc integrate() or proc go(); they do less than the standard run system's run(). run() and all of its related procedures and functions are available if you include
load_file("stdgui.hoc")
or
load_file("nrngui.hoc")
at the start of your program.
You'll still need your custom proc init(), which should be defined before you call run().