Passing strings as parameters in proc()

The basics of how to develop, test, and use models.
Post Reply
ssothro
Posts: 13
Joined: Mon Feb 06, 2006 5:12 pm

Passing strings as parameters in proc()

Post by ssothro »

Hi,
I am trying to define a procedure in a hoc file that will take as a parameter section names. E.g. If I have a soma and an axon section I would like to have something like that:

foo() {
$1.L=10
...
}

foo(soma)

Is there a way to do that? I tried to do the above implementation, but the compiler returned errors.

Thanks
ted
Site Admin
Posts: 6384
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Yes, but first ask whether that's really the best way to solve the larger
problem. It is difficult to imagine a circumstance in which passing section
names as strings would be the best solution for a problem.

If you only need to execute statements in the context of a section, then

Code: Select all

proc foo() {
  stmt1
  stmt2
  . . .
}

secname foo(arg1, arg2 . . .)
is preferable. Example:

Code: Select all

proc setgeom() {
  L = $1
  diam = $2
  nseg = $3
}

print "Before calling setgeom"
soma psection()
soma setgeom(1000, 1, 25)
print "After calling setgeom"
soma psection()
If you need to pass more than one section to a proc, use either a
SectionList or use SectionRefs, then pass the SectionList or SectionRefs.

If you absolutely _must_ pass a section name as a string, use sprint()
within the proc to create a command string, then use execute() to
execute the command. Example:

Code: Select all

strdef cmdstr

proc setnseg() { // but why bother??
  sprint(cmdstr,"%s nseg = %d", $s1, $2)
  execute(cmdstr)
}

setnseg("soma", 5)
But I bet you a nickel that one of the three other approaches is far
superior to this.
Post Reply