HOC exercises

// Executable lines below are shown with the hoc prompt // Typing these, although trivial, can be a valuable way to get familiar with the language

oc> // A comment

oc> /* ditto */

Data types: numbers strings and objects

Operators and numerical functions

oc> x=8 // assignment

oc> print x+7, x*7, x/7, x%7, x-7, x^7 // doesn't change x

oc> x==8 // comparison

oc> x==8 && 5==3 // logical AND, 0 is False; 1 is True

oc> x==8 \\ 5==3 // logical OR

oc> !(x==8) // logical NOT, need parens here

oc> print 18%5, 18/5, 5^3, 3*7, sin(3.1), cos(3.1), log(10), log10(10), exp(1)

oc> print x, x+=5, x*=2, x-=1, x/=5, x // each changes value of x; no x++

Blocks of code {}

oc> { x=7

print x

x = 12

print x

}

Conditionals

oc> x=8

oc> if (x==8) print "T" else print "F" // brackets optional for single statements

oc> if (x==8) {print "T"} else {print "F"} // usually better for clarity

oc> {x=1 while (x<=7) {print x x+=1}} // nested blocks, statements separate by space

oc> {x=1 while (x<=7) {print x, x+=1}} // notice difference: comma makes 2 args of print

oc> for x=1, 7 print x // simplest for loop

oc> for (x=1;x<=7;x+=2) print x // (init;until;change)

Procedures and functions

oc> proc hello () { print "hello" }

oc> hello()

oc> func hello () { print "hello" return 1.7 } // functions return a number

oc> hello()

Built-in object types: graphs, vectors, lists, files

File

oc> objref file

oc> mystr = "AA.dat" // use as file name

oc> file = new File()

oc> file.wopen(mystr) // 'w' means write, arg is file name

oc> vec.vwrite(file) // binary format

oc> file.close()

oc> vec[1].fill(0) // set all elements to 0

oc> file.ropen(mystr) // 'r' means read

oc> vec[1].vread(file)

oc> if (vec.eq(vec[1])) print "SAME" // should be the same

List

oc> objref list

oc> list = new List()

oc> list.append(vec) // put an object on the list

oc> list.append(g) // can put different kind of object on

oc> list.append(list) // pointless

oc> print list.count() // how many things on the list

oc> print list.object(2) // count from zero as with arrays

oc> list.remove(2) // remove this object

oc> for ii=0, list.count-1 print list.object(ii) // remember list.count, vec.size

Simulation

oc> create soma

oc> access soma

oc> insert hh

oc> ismembrane("hh") // make sure it's set

oc> print v, v(0.5), soma.v, soma.v(0.5) // only have 1 seg in section

oc> tstop=50

oc> run()

oc> print t, v

oc> print gnabar_hh

oc> gnabar_hh *= 10

oc> run()

oc> print t, v // what happened?

oc> gnabar_hh /= 10 // put it back

Recording the simulation

oc> cvode_active(0) // this turns off variable time step

oc> dt = 0.025

oc> vec.record(&soma.v(0.5)) // '&' gives a pointer to the voltage

oc> objref stim

oc> soma stim = new IClamp(0.5) // current clamp at location 0.5 in soma

oc> stim.amp = 20 // need high amp since cell is big

oc> stim.dur = 1e10 // forever

oc> run()

oc> print vec.size()*dt, tstop // make sure stored the right amount of data

Graphing and analyzing data

oc> g=new Graph()

oc> vec.line(g, dt, 2, 2)

oc> g.size(0, tstop, -80, 50)

oc> print vec.min, vec.max, vec.min_ind*dt, vec.max_ind*dt

oc> vec[1].deriv(vec, dt)

oc> print vec[1].max, vec[1].max_ind*dt // steepest AP

Roll your own GUI

oc> proc sety () { y=x print x }

oc> xpanel("test panel")

oc> xvalue("Set x", "x")

oc> xvalue("Set y", "y")

oc> xbutton("Set y to x", "sety()")

oc> xpanel()