Page 1 of 1
Changing hoc variables after xopen but before instantiation
Posted: Thu Aug 07, 2014 6:13 pm
by dopadelic
I'm using python to import hoc commands from a hoc file
h('xopen("CA1PyrCmHSpikeErrorAHPDepth.hoc")')
However, before instantiating a cell from the template within the hoc file, I want to change some of the variables inside that hoc file.
Here's the code I'm using to instantiate a new cell from the template.
h('cell = new celltemplate(econ)')
I tried h('variableval=10'), and this doesn't seem to access the variable inside the template. So how do I do so?
Thanks for this wonderful community!
Re: Changing hoc variables after xopen but before instantiat
Posted: Thu Aug 07, 2014 10:23 pm
by ramcdougal
One solution is to declare a top-level variable that is used for initialization and set that.
For example, consider example.hoc's use of initvalue:
Code: Select all
// default value for creating new Example objects
initvalue = 17
begintemplate Example
public value, print_value
external initvalue
proc print_value() {
printf("value = %g\n", value)
}
proc init() {
value = initvalue
}
endtemplate Example
Here's a Python routine that creates two Example instances, both of which have independent "value" variables, but both of which are initially set to a default value specified in the Python:
Code: Select all
from neuron import h, gui
# load the template
h.load_file('example.hoc')
# change the default value (was 17)
h.initvalue = 32
obj = h.Example()
# show the default value (32)
obj.print_value()
# change the value and re-show
obj.value = 5.15
obj.print_value()
#
# or to give the object a direct HOC name
#
h('objref cell')
h.cell = h.Example()
# prints the (Python-specified as above) default value of 32
h.cell.print_value()
As an aside, note that you don't need to write a string to access HOC functions and variables; they're accessible directly as members of the h module.
Re: Changing hoc variables after xopen but before instantiat
Posted: Fri Aug 08, 2014 2:49 pm
by ted
Parameters can also be passed at the time of instance creation.
Code: Select all
begintemplate Example
public print_value
proc print_value() {
printf("value = %g\n", value)
}
proc init() {
value = $1
}
endtemplate Example
objref elist
elist = new List()
for i=0,3 elist.append(new Example(i*i))
for i=0,elist.count()-1 {
print "item ", i
elist.o(i).print_value
}
I believe you can also write templates using numarg so that 0 or more parameters can be passed.
Re: Changing hoc variables after xopen but before instantiat
Posted: Sat Aug 09, 2014 8:20 pm
by dopadelic
Thank you ramcdougal, it works perfectly!
ted: good to see alternative ways of doing it!