Generate multiple files using loop statement

Anything that doesn't fit elsewhere.
Post Reply
yuzhou
Posts: 7
Joined: Fri Apr 29, 2016 10:44 pm

Generate multiple files using loop statement

Post by yuzhou » Fri Sep 09, 2016 5:19 pm

Here's the hoc code

Code: Select all

strdef tname

for a=1,3 {

  objref savedata
  savedata = new File()

  sprint(tname, "file%d.dat", a)
  savedata.wopen(tname)

  savedata.printf (tname, "\n")
  
  savedata.close()
}
Run this script in Nenuron and I got following information without any file generated:

oc>load_file("temp.hoc")
nrniv: syntax error
in temp.hoc near line 9
savedata = new File()
^
xopen("temp.hoc")
execute1("{xopen("te...")
load_file("temp.hoc")
0

Help please.

yuzhou
Posts: 7
Joined: Fri Apr 29, 2016 10:44 pm

Re: Generate multiple files using loop statement

Post by yuzhou » Sat Sep 10, 2016 11:40 am

The code works if I move "objref savedata" before the loop block:

Code: Select all

strdef tname

objref savedata
for a=1,3 {

  printf("Simulation %d .\n", a)
    savedata = new File ()

  sprint(tname, "file%d.dat", a)
  savedata.wopen(tname)

  savedata.printf(tname, "\n")
  
  savedata.close()
}

quit()
Now the script generates three files as expected. But I still want to know why I cannot put objref statement in a loop.

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

Re: Generate multiple files using loop statement

Post by ted » Mon Sep 12, 2016 11:41 am

In hoc all user-created variables are treated as scalars (simple double precision variable) unless they have already been declared to be something else. That declaration must be done outside of any proc, func, or compound statement (a compound statement is one or more statements that appear inside a pair of curly brackets { } ). After a variable has been declared to be an objref, it can appear in another objref statement inside a proc or func or compound statement.

yuzhou
Posts: 7
Joined: Fri Apr 29, 2016 10:44 pm

Re: Generate multiple files using loop statement

Post by yuzhou » Mon Sep 12, 2016 4:41 pm

Thanks, Ted.

Post Reply