how to read single line from a file in HOC

Particularly useful chunks of hoc and/or NMODL code. May be pedestrian or stunningly brilliant, may make you gasp or bring tears to your eyes, but always makes you think "I wish I had written that; I'm sure going to steal it."
Post Reply
breakwave922
Posts: 74
Joined: Mon May 19, 2014 10:39 pm

how to read single line from a file in HOC

Post by breakwave922 » Thu Aug 09, 2018 12:07 pm

I have a txt file with a collection of vectors with different length. Vectors are stored in each line of this file.
Is there any way to read this file line by line in HOC? Because different lines have different lengths, I cannot use loadmatrix and getrow.

Thanks in advance.

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

Re: how to read single line from a file in HOC

Post by ted » Fri Aug 10, 2018 11:46 am

Consider
scanf
scanstr
scantil
scanvar
or maybe even fscan.
Read about them in the Programmer's Reference.

breakwave922
Posts: 74
Joined: Mon May 19, 2014 10:39 pm

Re: how to read single line from a file in HOC

Post by breakwave922 » Fri Aug 10, 2018 2:56 pm

Ted,

Thanks for the reply. However, I still couldn't figure out how to read line-by-line from a file.
For example, I have a txt data file with 3 lines, and data looks like:

12, 33
22, 3, 4
2, 3, 5, 7

If I want to read only 2nd line to have a vector with value [22,3,4], I tried scanf and fscan, and it read all the values (9 values in the example) from the file into one vector. Then, I need to use vector.at(start,end) to segregate them to get values for a particular line I want.

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

Re: how to read single line from a file in HOC

Post by ted » Sat Aug 11, 2018 9:38 am

String parsing is not hoc's long suit. Read everything you can about hoc's StringFunction class, and study the file-related methods mentioned in my previous post. If a possible solution doesn't jump out at you, then figure out some other way to do it. Python has powerful string parsing tools. Or maybe you would prefer to process your strangely structured data file with some other software.

Post Reply