vector.record() question

Anything that doesn't fit elsewhere.
Post Reply
Prokopiou

vector.record() question

Post by Prokopiou » Mon Jan 30, 2012 11:44 am

Hello all,

I might be asking something that is obvious but my mind is stuck.
I found similar code in other forum questions that should be working as I was expecting it to work but I get results I don't expect.

Code: Select all

objref test1
objref test2
objref results_list
objref tmpvec 

proc record_results() { 
	results_list = new List()
	tmpvec = new Vector()

	forall {
			
			tmpvec.record(&v(0.5))
			results_list.append(tmpvec)
		
		}
		

    test1 = new Vector()
test2 = new Vector()
	test1.record(&cen_myelin[19].v(0.5))
	test2.record(&cen_myelin[0].v(0.5))

}

record_results() // This saves the internal voltage of each segment in a list
The two test vectors (test1 and test2) work as expected giving the time evolution of the two section centres they are recording.

The results_list I expected to have a list with all the time evolution of the sections in my model. Instead I do get a list that has the correct number of sections but it holds only the last section's recording.

more specifically, i have 55 sections and this list has 55 identical vectors and they all correspond to the last section.
Why is this? Instead of passing a value I pass a reference that gets updated every time resulting in the same ouput?

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

Re: vector.record() question

Post by ted » Mon Jan 30, 2012 11:03 pm

You're off to a good start--only a minor repair is needed.

The cause of the problem is that tmpvec is pointing to the very same Vector instance on every step of the forall statement. So List ends up full of copies of the same objref, each one pointing to the same instance of the Vector class, and that Vector records whatever the last Vector.record statement told it to record.

You want to revise your code so that it looks like this:

Code: Select all

objref results_list

proc record_results() { localobj tobj
  results_list = new List()
  forall {
    tobj = new Vector()
    tobj.record(&v(0.5))
    results_list.append(tobj)
  }
 . . .

Prokopiou

Re: vector.record() question

Post by Prokopiou » Tue Jan 31, 2012 6:08 am

Hello Ted,

Thank you! The code runs as it is supposed to now. I tried reassigning the vector to a Null after the for loop finished so it didn't occur to me that it had to be in a loop.
Monday is by far the worst day to try and code :)

Thank you again,
Andreas.

Post Reply