Page 1 of 1

Working with sections in procedures

Posted: Wed Aug 29, 2012 12:25 pm
by JimH
I was hoping to create a procedure which took an array of sections and connects them end to end. I was hoping to declare this procedure on startup. The latter statement is important because from what I gather procedures and functions have access to their parent's workspace, meaning if I previously define a section object before a procedure, I can work with that variable in the procedure without passing it in explicitly.

After a bit of searching I came across the class SectionRef, which I was hoping would accomplish what I want. However it appears as though SectionRef only applies to a single section, not an array of sections. Using the class functionality I can access children and parents of the section, but these don't exist until I connect them, which is what I am trying to do with the procedure.

Is there any way of creating a self contained procedure which accomplishes what I would like? Alternatively, if I am stuck with relying on the procedure being open to the parent, do I create a dummy section before defining the procedure, and then rely upon redefining that section before actually calling the procedure?

i.e. something like
NOTE: I would probably pass in n_sections. Ideally I could query this from the axon_sections ...

Code: Select all

create axon_sections
proc connect_axon(){ 
  for i = 0, n_sections - 2{
    connect axon_sections[i+1](0), axon_sections[i](1)
  }
}

//DO THINGS HERE
create axon_sections[n_sections]
//DEFINE SECTIONS HERE
proc connect_axon()
Thanks,
Jim

Re: Working with sections in procedures

Posted: Wed Aug 29, 2012 5:00 pm
by ted
If all the sections that are to be connected have the same root name, e.g. axon, you could

NAX = however many you want
create axon[NAX]
for i=1,NAX-1 connect axon(0), axon[i-1](1)

The extension to alternating between two different kinds of sections, e.g. nodes and internodes, is easy.

Were you looking for something that would produce a branched architecture, or assembled an arbitrary sequence of sections?

Re: Working with sections in procedures

Posted: Wed Aug 29, 2012 5:08 pm
by JimH
I was thinking about putting that small bit of code within a procedure. I can't seem to define a procedure with sections unless those sections already exist. It seems like I can work with sections in procedures if I use sectionRef(). The sectionRef doesn't appear to allow indexing into a section array though. At this point I'm going with the simple code (basically what you provided).

Thanks,
Jim

Re: Working with sections in procedures

Posted: Wed Aug 29, 2012 6:09 pm
by ted
JimH wrote:I was thinking about putting that small bit of code within a procedure. I can't seem to define a procedure with sections unless those sections already exist.
(1) Can be a worthy aim--cleaner code and all that. (2) True. However, you can always do something like this:

Code: Select all

create axon[1]
/*
  hoc now knows that axon is an "array" of 1 or more sections
  and this has two implications
  1. it can be used in another create statement 
  that changes the number of sections in this "array"
  2. such a create statement can be executed in a proc or func
*/
func makeaxon() { local i
  if (i<2) {
    print "argument must be >= 2"
    return 0 // result signifies failure
  } else {
    create axon[$1]
    for i=1,$1 connect axon[i](0), axon[i-1](1)
    return $1 // > 0 result signifies success
  }
}