Dear all,
Could any of you give me any clue about:
a.)How to assign the name of a previously defined string to a NEURON variable/object?
b.) How can I get the string of the name of any object or variable and compare it with another string?
Thanks in advance,
strings as variable's name...
Re: strings as variable's name...
b) To compare strings use strcmp
SYNTAX
x = strcmp("string1", "string2")
DESCRIPTION
return negative, 0, or positive value depending on how the strings compare lexicographically. 0 means they are identical.
Example
if ((strcmp(secname(),"dend10[9]"))==0){
stimuli = new AlphaSynapse(abs(inverted - rel_pos))
print "alphasynapse inserted in ",secname()
}
SYNTAX
x = strcmp("string1", "string2")
DESCRIPTION
return negative, 0, or positive value depending on how the strings compare lexicographically. 0 means they are identical.
Example
if ((strcmp(secname(),"dend10[9]"))==0){
stimuli = new AlphaSynapse(abs(inverted - rel_pos))
print "alphasynapse inserted in ",secname()
}
-
- Site Admin
- Posts: 6384
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
Re: strings as variable's name...
It is not clear whether you are asking how to use a string as the name of a strdef, double, or objref, or whether the question is how to use a string as a _second_ name for an existing strdef, double, or objref.OJAG wrote:a.)How to assign the name of a previously defined string to a NEURON variable/object?
If your question is the former: the idiom is
strdef cmd
sprint(cmd, format string, variable names)
execute(cmd)
For example,
strdef foo
foo = "whatever"
strdef cmd
sprint(cmd, "%s = %f", foo, sqrt(2))
execute(cmd)
creates a new double called whatever with the value 1.414214...
For objects, there must be two sprint/execute pairs. The first pair
sprint(cmd, "objref %s", foo)
execute(cmd)
is needed to make whatever be an objref. The second pair
sprint(cmd, "%s = new SomeClassName()", foo)
is needed to make whatever point to an instance of SomeClassName.
If your question is the latter, here is your answer: in hoc there is no way to make multiple names refer to the same double or strdef. However, multiple objref names may refer to a single object.
objref bar
bar = new Vector()
strdef foo
foo = "whatever"
strdef cmd
sprint(cmd, "objref %s", foo)
execute(cmd)
sprint(cmd, "%s = %s", foo, "bar")
execute(cmd) // the command is "whatever = bar"
print bar // demonstrate that foo and bar refer to the same Vector instance
print whatever