object name substitution

The basics of how to develop, test, and use models.
Post Reply
Koenv

object name substitution

Post by Koenv »

I was wondering how to do the following:

I have created e.g. 3 synapses called syn1, syn2 and syn3. All are dual exponential synapses.

In a loop I would like to change e.g. tau1 of these 3 synapses to different values, like:

for cnt = 1, 3 {
...
syncnt.tau1 = cnt * 5
...
}

Thus, in this loop I'd like to have the counter refer to the different object names and change their respective tau1. Or how can I construct the name of an object with a counter and change some parameters of this object? Or said differently; how can I concatenate a prefix and a cnt value so that it make a object name. Of course, one could say create the synapse with: objectvar syn[3] instead but lets assume this is not an option.

thanks,

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

Dealing with multiple instances of a class

Post by ted »

You have a choice of two different approaches.

You could use an "array" of objrefs.
objref syn[n]
declares a collection of objrefs whose names are syn[0]...syn[n-1]
so you could do
for i = 0,n-1 syn.param = f(i)

Or you could use a List. This takes a bit of planning, but it is far more flexible than using "arrays" of names. For one thing, you never have to remember "magic numbers" like how many different syn[] things you have. You start by creating a List, then every time you create a new instance of your synaptic mechanism, you append it to the list.

Code: Select all

objref synlist, tmpobj
synlist = new List()
section1 tmpobj = new Synmech(range1)
synlist.append(tmpobj)
section2 tmpobj = new Synmech(range2)
synlist.append(tmpobj)
etc.
When it comes time to assign properties to these mechanisms, iterate over each object in synlist like so

Code: Select all

for i = 0, synlist.count()-1 {
  // statements that specify synlist.o(i) parameter values, e.g.
  synlist.o(i).param = f(i)
}
This takes a bit more typing, but the resulting code is much easier to maintain and is free of magic numbers.

Taking this one step further, a way to really clean up this code would be to start by appending the sections of interest to a SectionList or a List of SectionRefs, then iterate over those to create and attach the synaptic mechanisms.
Koenv

Re: object name substitution

Post by Koenv »

Hi,

I just got know an alternative method that one actually wants to avoid but it works if necessary.
I wanted to add that when one cannot change the names of these synapses then the loop could look like this:

strdef cmd
for cnt = 1, 3 {
sprint(cmd,"syn%d.tau1 = (%d * 5)",cnt, cnt)
execute(cmd)
}
ted
Site Admin
Posts: 5810
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: object name substitution

Post by ted »

Almost. The "'for' loop" should start at 0. Remember that in hoc, as in C and many other programming languages, the index of the first item is 0, not 1.
Post Reply