Page 1 of 1

vector.play doesn't work when executed inside of proc

Posted: Wed Oct 23, 2013 7:37 am
by Bill Connelly
I was starting to write this function to allow me to play in arbitrary waveforms via IClamp (starting with simple EPSCs, with a mind to build up to chirp/zap functions). The code works when I enter it directly into the nrn window, but when I try to execute it as a function, it doesn't work (no errors. the IClamp just doesn't do anything). My feeling is this is something to do with the scope that the vector has, i.e. it is local to the function preparePlayVector() and hence it is destroyed by the time finitialize() is called.. But I'm trying to be a good coder and avoid global variables. Is there a way to execute vector.play() with local variables?

Code:

Code: Select all

objref somastim
somastim = new IClamp(0.5)
somastim.del = 0
somastim.dur = 1e9

obfunc createEPSC() {local factor, n, t localobj oneEPSC //$1=rise, $2=decay, $3=amplitude
  oneEPSC = new Vector(10001)
  factor = (($2/$1)^($2/($1-$2)))*(($2-$1)/$1)
  for (n=0; n<10001; n+=1) {
    t = n*dt // n = sample number, t = time in ms
    oneEPSC.x(n) = ($3)/factor*(exp(-t/$2)-exp(-t/$1))
  }
  return oneEPSC
}

proc preparePlayVector() {local n, EPSCdel localobj summedEPSC //$1=number of EPSCs, $2=decay
  EPSCdel = 1000
  summedEPSC = new Vector(tstop/dt)
  summedEPSC.copy(createEPSC(.5, 3, 20), EPSCdel/dt)
  summedEPSC.play(&somastim.amp, dt) //HERE IS THE PROBLEM!!!
}

preparePlayVector()

Re: vector.play doesn't work when executed inside of proc

Posted: Wed Oct 23, 2013 1:03 pm
by ted
I'm trying to be a good coder and avoid global variables.
And, in so doing, fell into the trap of turning a virtue into a vice.

As you guessed, the cause of the problem is that the localobj summedEPSC vanishes upon exit from proc preparePlayVector(), before you can run a simulation. You could insert a run() statement at the end of preparePlayVector(), but it's more pragmatic to declare summedEPSC at the top level and be done with it. Given that its name is so mnemonic, there's little chance that you'd accidentally misuse it for some other purpose.

Besides, there always has to be _something_ that exists at the top level, even if it is an object instance that contains all the other code that you write. Otherwise there couldn't be any user-written code at all.

Re: vector.play doesn't work when executed inside of proc

Posted: Wed Oct 23, 2013 1:06 pm
by ted
Bill Connelly wrote:with a mind to build up to chirp/zap functions
You might find this to be useful
viewtopic.php?f=8&t=2404&p=9483#p9483