1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
.. _hoc_secspec:
.. _hoc_CurrentlyAccessedSection:
Section and Segment Selection
-----------------------------
Since sections share property names eg. a length called :hoc:data:`L`
it is always necessary to specify
*which* section is being discussed.
There are three methods
of specifying which section a property refers to (with each being
compact in some contexts and cumbersome in others). They are given
below in order of precedence (highest first).
Dot notation
~~~~~~~~~~~~
This takes precedence over the other methods and
is described by the syntax :samp:`{sectionname}.varname`. Examples
are
.. code-block::
none
dendrite[2].L = dendrite[1].L + dendrite[0].L
axon.v = soma.v
print soma.gnabar
axon.nseg = 2*axon.nseg
This notation is necessary when one needs to refer to more than
one section within a single statement.
Stack of sections
~~~~~~~~~~~~~~~~~
The syntax is
.. code-block::
none
sectionname {stmt}
and means that the currently selected section during the
execution of *stmt*
is *sectionname*. This method is the most useful for
programming since the user has explicit control over
the scope of the section and can set several range variables.
Notice that after the *stmt* is executed the currently selected
section reverts
to the name (if any) it had before *sectionname* was seen. The
programmer is allowed to
nest these statements to any level.
Avoid the error:
.. code-block::
none
soma L=10 diam=10
which sets ``soma.L``, then pops the section stack and sets :hoc:data:`diam`
for whatever section is then on the stack.
It is important that control flow reach the end of *stmt* in order to
automatically pop the section stack. Therefore, one cannot use
the :ref:`continue <hoc_keyword_continue>`, :ref:`break <hoc_keyword_break>`, or :ref:`return <hoc_keyword_return>` statements in *stmt*.
There is no explicit notion of a section variable in NEURON but the same
effect can be obtained with the :hoc:class:`SectionRef` class. The use of :hoc:func:`push_section`
for this purpose is not recommended except as a last resort.
Looping over sets of sections is done most often with the :ref:`forall <hoc_keyword_forall>` and :ref:`forsec <hoc_keyword_forsec>`
commands.
Default section
~~~~~~~~~~~~~~~
The syntax
.. code-block::
none
access sectionname
defines a default section name to be the currently selected section when the
first two methods are not in effect. There is often a conceptually
privileged section which gets most of the use and it is useful to
declare that as the default section. e.g.
.. code-block::
none
access soma
With this, one can, with a minimum of typing, get values of voltage, etc
at
the command line level.
In general, this statement should only be used once to give default access
to a privileged section. It's bad programming practice to change the
default access within anything other than an initialization procedure.
The "``sec { stmt }``" form is almost always the right way to
use the section stack.
----
.. index:: access (keyword)
.. _hoc_keyword_access:
**access**
Syntax:
``access section``
Description:
Makes *section* the default currently accessed section.
More precisely, it replaces the top of the section stack with the
indicated section and so will be the permanent default section only if
the section stack is empty or has only one section in it.
This is lesser
precedence than
``section stmt``
which is lesser precedence than
``section.var``
Note:
The access statement should not be used within a procedure or function. In
fact the best style is to execute it only once in a program to refer to
a priviledged section such as "soma". It can be very confusing when a
procedure has the side effect of permanently changing the default section.
Example:
.. code-block::
none
create a, b, c, d
access a
print secname()
b {
print secname()
access c // not recommended. The "go_to" of sections.
print secname()
d {print secname()}
print secname()
} // because the stack has more than one section, c is popped off
print secname() // and the second "access" was not permanent!
----
.. index:: forall (keyword)
.. _hoc_keyword_forall:
**forall**
Syntax:
``forall stmt``
Description:
Loops over all sections, successively making each section the currently
accessed section.
Within an object, ``forall`` refers to all the sections
declared in the object. This is generally the right thing to do when a template
creates sections but is inconvenient when a template is constructed which
needs to compute using sections external to it. In this case, one can pass a collection
of sections into a template function as a :hoc:class:`SectionList` object argument.
The ``forall`` is relatively slow,
especially when used in conjunction with :hoc:func:`issection`
and :hoc:func:`ismembrane` selectors. If you are often iterating over the same
sets it is much faster to keep the sets in :hoc:class:`SectionList` objects and use
the much faster :ref:`forsec <hoc_keyword_forsec>` command.
The iteration sequence order is undefined but will remain the same for
a given sequence of :ref:`create <hoc_keyword_create>` statements.
Example:
.. code-block::
none
create soma, axon, dend[3]
forall {
print secname()
}
prints the names of all the sections which have been created.
.. code-block::
none
soma
axon
dend[0]
dend[1]
dend[2]
.. seealso::
:ref:`forsec <hoc_keyword_forsec>`, :ref:`ifsec <hoc_keyword_ifsec>`, :hoc:func:`issection`, :hoc:func:`SectionList`, :hoc:func:`ismembrane`
----
.. index:: ifsec (keyword)
.. _hoc_keyword_ifsec:
**ifsec**
Syntax:
``ifsec string stmt``
``ifsec sectionlist stmt``
Description:
ifsec string stmt
Executes stmt if string is contained in the name of the currently
accessed section. equivalent to :samp:`if(issection({string}))` stmt
Note that the regular expression semantics is not the same as that
used by issection. To get an exact match use
ifsec ^string$
ifsec sectionlist stmt
Executes stmt if the currently accessed section is in the sectionlist.
.. seealso::
:ref:`forsec <hoc_keyword_forsec>`, :hoc:class:`SectionList`, :hoc:func:`issection`
----
.. index:: forsec (keyword)
.. _hoc_keyword_forsec:
**forsec**
Syntax:
``forsec string stmt``
``forsec sectionlist stmt``
Description:
forsec string stmt
equivalent to ``forall ifsec string stmt`` but faster.
Note that forsec string is equivalent to
:samp:`forall if (issection({string})) stmt`
forsec sectionlist
equivalent to ``forall ifsec sectionlist stmt`` but very fast.
These provide a very efficient iteration over the list of sections.
Example:
.. code-block::
none
create soma, dend[3], axon
forsec "a" print secname()
.. code-block::
none
create soma, dend[3], axon
objref sl
sl = new SectionList()
for (i = 2; i >= 0; i = i - 1) dend[i] sl.append()
forsec sl print secname()
----
.. hoc:function:: pop_section
Syntax:
``pop_section()``
Description:
Take the currently accessed section off the section stack. This can only be used after
a function which pushes a section on the section stack such as
``point_process.getloc()``.
Example:
.. code-block::
none
create soma[5]
objref stim[5]
for i=0,4 soma[i] stim[i] = new IClamp(i/4)
for i=0,4 {
x = stim[i].get_loc()
printf("location of %s is %s(%g)\n", stim[i], secname(), x)
pop_section()
}
----
.. hoc:function:: push_section
Syntax:
``push_section(number)``
``push_section(section_name)``
Description:
This function, along with ``pop_section()`` should only be used as a last resort.
It will place a specified section on the top of the section stack,
becoming the current section to which all operations apply. It is
probably always better to use :hoc:class:`SectionRef`
or :hoc:class:`SectionList` .
:samp:`push_section({number})`
Push the section identified by the number returned by
this_section, etc. which you desire to be the currently accessed
section. Any section pushed must have a corresponding pop_section()
later or else the section stack will be corrupted. The number is
not guaranteed to be the same across separate invocations of NEURON.
:samp:`push_section({section_name})`
Push the section identified by the name obtained
from sectionname(*strdef*). Note: at this time the implementation
iterates over all sections to find the proper one; so do not use
in loops.
.. seealso::
:hoc:class:`SectionRef`
|