iterating over selected sections

Anything that doesn't fit elsewhere.
Post Reply
stephanmg
Posts: 68
Joined: Tue Jul 03, 2012 4:40 am

iterating over selected sections

Post by stephanmg » Sun Oct 27, 2013 9:16 am

How can i translate

Code: Select all

forall for i=0,n3d()-1 fprint("%f %f %f %f\n", x3d(i), y3d(i), z3d(i), v(i)) 
to something like:

Code: Select all

for i=0, all-sections()
 for j =0, n3d() -1
fprint("%f %f %f %f\n", x3d(i), y3d(i), z3d(i), v(i)) 
Best regards,
Stephan

hines
Site Admin
Posts: 1577
Joined: Wed May 18, 2005 3:32 pm

Re: iterating over selected sections

Post by hines » Mon Oct 28, 2013 10:56 am

I may not understand your question. If it refers to executing a multline string, then, sadly, that is not supported. I is often possible, though, to execute "load_file(\"filename.hoc\")"
that provides a number of functions easily callable from a single line.

if it refers to the details of 'for i=0, all-sections()', i'd refer you to SectionList and SectionRef in the documentation.
http://www.neuron.yale.edu/neuron/stati ... ectionList
http://www.neuron.yale.edu/neuron/stati ... SectionRef

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

Re: iterating over selected sections

Post by ted » Mon Oct 28, 2013 12:08 pm

A question that may answer your question: are you just looking for hoc's syntax for creating compound statements? paired curly brackets?

Code: Select all

forall {
  for i=0,n3d()-1 {
    fprint("%f %f %f %f\n", x3d(i), y3d(i), z3d(i), v(i))
  }
}[/quote]You might find this helpful:
[url]http://www.neuron.yale.edu/neuron/static/new_doc/programming/hocsyntax.html[/url]

stephanmg
Posts: 68
Joined: Tue Jul 03, 2012 4:40 am

Re: iterating over selected sections

Post by stephanmg » Mon Oct 28, 2013 12:19 pm

Dear hines, dear ted,

I'm sorry, my question was misleading.

I know that i can iterate over all sections with the "forall" construct.

I tried to access a section by an index... let's say we have:

Code: Select all

objref all_sections
all_sections = new SectionList()
total_sections = 0
forall { all_sections.append() }
forall { total_sections = total_sections + 1 }
Now i want to do something like:

Code: Select all

j = 10

if (j < total_sections) {
   access all_sections[j];
   // do something
}
Could this be possible somehow?

All the best,
Stephan

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

Re: iterating over selected sections

Post by ted » Mon Oct 28, 2013 12:53 pm

You could

Code: Select all

i = 0
forall {
  if (i<NUM) do whatever
  i+=1
}
If you're going to do this many times in the course of program execution, it might be faster to first set up a SectionList that contains just the first NUM sections e.g.

Code: Select all

objref first10
first10 = new SectionList()
i = 0
forall {
  if (i<10) first10.append()
  i+=1
}
Then you'll be able to

Code: Select all

forsec first10 whatever
It is an accident of the history of NEURON's evolution that sections were added to hoc before object oriented programming was added. This is why you can't simply append a section to a List, then use somelist.o(i) to refer to the i+1th section in somelist. But there is a workaround if you really must have indexed access to particular sections: make a List of SectionRefs.

Code: Select all

objref srlist
srlist = new List()
forall srlist.append(new SectionRef()) // for each section
  // create a new SectionRef that refers to that section
  // and append that SectionRef to srlist
Then
srlist.o(i) is the i+1st SectionRef in srlist
srlist.o(i).ref is the section to which that SectionRef refers
srlist.o(3).ref print secname() // prints the name of the 4th section, and

Code: Select all

for i = 2,5 srlist.o(i).ref {
  statements that refer to variables that belong to sections 3-6
}
Comment:
Referring to sections by index number is seldom necessary. Usually it is better to construct one or more custom SectionLists and iterate over their contents.

stephanmg
Posts: 68
Joined: Tue Jul 03, 2012 4:40 am

Re: iterating over selected sections

Post by stephanmg » Mon Oct 28, 2013 1:17 pm

Ted,

thank you so much!
You just explained "my" workaround I was considering bad practice, which I already implemented exactly the way you are describing it!
Perfect, thanks for your affirmation.

And thanks again for your work and time supporting me.

NB: Another possibilty would be if i could globally index all points which were added by pt3dadd() -> since i'm using this (which seems to me that the points of each section have a local index within the section?):

Code: Select all

access section_of_interest
for i=0, n3d()-1 { 
  // do something 
}
All the best,
Stephan

Post Reply