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.
how to read single line from a file in HOC
-
- Site Admin
- Posts: 6384
- 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
Consider
scanf
scanstr
scantil
scanvar
or maybe even fscan.
Read about them in the Programmer's Reference.
scanf
scanstr
scantil
scanvar
or maybe even fscan.
Read about them in the Programmer's Reference.
Re: how to read single line from a file in HOC
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.
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.
-
- Site Admin
- Posts: 6384
- 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
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.