Specman how to read a specific line from a file, with no loop - specman

I have a long file, and I want to read a specific line not from the first sequence lines.
is there a way to do it without looping all over the file and counting the lines?
For example files.read, which get an index from which line to read?
Thanks

You can use the predefined method files.get_text_lines().

Related

Extract Specific Line Number in text file with MATLAB

I have a file that is nth lines long and I want to extract line 10 from the file and read it in as a string. I don't want to import the file, I don't want to search for a string in the file, and I don't want to skip nth lines, I just want to read in line 10. I'm having trouble scripting this up, how can I do this?
fileID = fopen(test.txt','r');
fclose(fileID)
If you knew exactly how many bytes into the file line 10 was, you could use fseek to skip to that offset in file. If you do not know this, then you have no other option than to read line by line using fgetl and ignore lines until you get to line 10.
Matlab can't find the nth line without linearly searching for eol characters. Even if a function did exist to go to line 10, that function would still need to read every line and check for eol. You have to either skip n lines use fgets/fgetl or use fseek if you know how many bytes precede the line.

How to read data from txt between 2 lines with the same symbol?

I have a lot of .txt files (<1000 lines each). The data format is the following (the picture): there are some lines in the beginning that I don't need, then the line with '', then the lines with data that I need to extract from the file, then again a line with '' and some comments that I don't need.
Is there any way to do that? I have a lot of such files. The matter is that in every file the number of lines before the first '' is different. So, is there any way to read the data in between of two ''? I tried all the functions but I am a beginner and just cannot come up with the right idea...
This is quite simple with regular expressions:
usefulData = regexp(fileread('abg06.txt'), '(?<=\*).*?(?=\*)', 'match','once');

Regarding Capture in Stata

I have code that I mostly took from here (bottom column): http://www.ats.ucla.edu/stat/stata/faq/append_many_files.htm
clear
file open myfile9 using C:\Users\RNCZF01\Documents\Cameron-Fen\Economics-Projects\Neighborhood-Project\list.csv, read
file read myfile9 line
insheet using `line', comma
save `line'.dta, replace
save master_data.dta, replace
drop _all
file read myfile9 line
while r(eof)==0 {
capture insheet using `line', comma
if _rc!=0 {
insheet using `line', comma
save `line'.dta, replace
append using master_data.dta, force
save master_data.dta, replace
}
drop _all
file read myfile9 line
}
Originally I had insheet using line', comma (I removed the back tick before the line because it was interfering with formatting). But the problem was that some of my sheets I was attempting to read were blank and so Stata would close. Thus I changed that to this:
capture insheet using `line', comma
if _rc!=0 {
insheet using `line', comma
However this closes after only reading the first document (and exits in the while loop before the first iteration of the while loop (second document) is done). My thought was that macros may disappear when they are used but I have no idea.
The reason your loop is closing is because you want
if _rc==0
for the inner loop. I am guessing your first file is not found, insheet throws up an error, and you are triggering the if _rc!=0 condition. Then the loop tries to run insheet without the capture and errors out. Another way of diagnosing this would be to run
set trace on
which I have found helpful in these sort of situations.
P.S. Not sure of the etiquette, but credit for this answer goes to lmo. I just thought it was worth writing up as an answer rather than a comment.

Load txt-file with two different delimiters into struct

I'm having trouble with loading .txt file in Matlab. The main problem is having not equal rows. I'll attach the file so you can more clearly see what I'm truing to say. First, the file has information about each node in graph. One row has information like this:
1|1|EL_1_BaDfG|4,41|5,1|6,99|8,76|9,27|13,88|14,19|15,91|19,4|21,48...
it means:
id|type|name|connected_to, weight|connected_to, weight| and so on..
I was trying to use fscanf function, but it only reads whole line as one string. How I suppose to divide it into struct with information that I need?
Best regards,
Dejan
Here, you can see file that I'm trying to load
An alternative to Stewie answer is to use:
fgetl to read each line
Then use
strread (or textscan) to split the string
Firstly using the | delimiter - then on the sub section(s) containing , do it a second time.

In Matlab, how do I create a CSV file from a subset of the lines in a text file?

I need to open a text file and convert it into a CSV file in Matlab. The first 3 lines of the text file are sentences that need to be omitted. The next 28 lines are numbers that need to make up the first column of the CSV, and then the next 28 lines need to make up the second column.
The text file is called datanal.txt and the output file can be named anything. Any help would be appreciated.
Don't have Matlab now to test, but try this. Your input file should be in Matlab's current directory, or put the full path to the file name.
A = csvread('datanal.txt',3,0);
A = reshape(A,28,2);
csvwrite('output.csv',A)
well you can add #'s in front of the first 3 lines then use load and a reshape. Did you need a fully automated script or is there only one file? If you're familiar with matlab at all there are a bunch of ways to turn that large column vector into a matrix.