Writing contents of vector to a file?

The basics of how to develop, test, and use models.
Post Reply
oregoneuron

Writing contents of vector to a file?

Post 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.
ted
Site Admin
Posts: 6299
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Writing contents of vector to a file?

Post 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.
oregoneuron

Re: Writing contents of vector to a file?

Post 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?
ted
Site Admin
Posts: 6299
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Writing contents of vector to a file?

Post 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().
Post Reply