Page 1 of 1

how to read single line from a file in HOC

Posted: Thu Aug 09, 2018 12:07 pm
by breakwave922
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.

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

Posted: Fri Aug 10, 2018 11:46 am
by ted
Consider
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

Posted: Fri Aug 10, 2018 2:56 pm
by breakwave922
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.

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

Posted: Sat Aug 11, 2018 9:38 am
by ted
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.