How to read multiple data values into an array

The basics of how to develop, test, and use models.
Post Reply
jbeard
Posts: 1
Joined: Mon Feb 18, 2019 4:52 am

How to read multiple data values into an array

Post 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.
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to read multiple data values into an array

Post 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 https://neuron.yale.edu/neuron/static/d ... 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
etc.
syntax.

"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
Then

Code: Select all

foo.printf()
will generate this output

Code: Select all

3.14159	1.41421	0.5	
	3 
Make sure you read about the Vector class and its methods--especially insrt and printf--in the Programmer's Reference for hoc
https://www.neuron.yale.edu/neuron/stat ... index.html
Post Reply