Page 1 of 1

How to read multiple data values into an array

Posted: Mon Feb 18, 2019 11:46 am
by jbeard
I am rather new to the hoc programming language, and I was wondering how I can assign a list of values to an array all in one go. For example, I want to assign various neuron lengths to a variable length_neuron, so I think I want something like this:

double length_neuron[3]

length_neuron = {40, 56, 64, 70}

So that the array length_neuron contains the values 40, 56, 64, 70. However, I don't know what the appropriate syntax is for this (as you can almost certainly tell).

Is it possible to do this with just two lines of code, or is it necessary to create a loop to assign each element individually? The latter seems a rather clumsy solution.

Re: How to read multiple data values into an array

Posted: Mon Feb 18, 2019 2:35 pm
by ted
You might find it easier to set up arrays with Python. Whether you are or aren't already familiar with Python, you might consider learning about both Python and NEURON by working through this tutorial ... index.html.

If you prefer to stick with hoc, then first let me strongly advise you to use an instance of hoc's Vector class for any purpose that involves a dimensional "array" of numbers. In fact, I can't think of a situation in which it would be advisable to use the old
double foo
foo[0] = scalar1
foo[1] = scalar2

"So how do I do this with an instance of hoc's Vector class?"

Code: Select all

objref foo // create an objref that will be used to reference a Vector
foo = new Vector() // foo is now a Vector with length == 0
foo.insrt(0, PI, sqrt(2), sin(PI/6)) // use the Vector class's insrt method

Code: Select all

will generate this output

Code: Select all

3.14159	1.41421	0.5	
Make sure you read about the Vector class and its methods--especially insrt and printf--in the Programmer's Reference for hoc ... index.html