sscanf how to skip lines - not textscan!!!
조회 수: 10 (최근 30일)
이전 댓글 표시
Is there a way to skip the first X lines in a text file when reading a text file using sscanf? I know this can be done using textscan but I've really struggled to get to grips using it for my particular situation and was wondering if there's an easy way to do it for sscanf.
My text file looks like:
bla bla bla bla
{
position 99.35 56.73 116.41
gamma_angle 90
weight 0.800000
collimator_setup 16 16 16 8 16 16 16 8
}
With the latter part in '{}' repeated many times. My current code looks like this:
str = fileread('filename.txt');
mat = sscanf(str,'{\r\r position %f %f %f\r gamma_angle %f\r weight %f\r collimator_setup %f %f %f %f %f %f %f %f\r\r}\r\r\r\r', [13,Inf]).';
The code works perfectly when the 'bla bla bla bla' is removed and returns a table of 13 collumns with the positions, angles etc...
However when any text is in front of the first '{', the code no longer works.
Thanks in advance.
댓글 수: 4
dpb
2020년 11월 3일
Give us a sample file...only needs contain the pertinent parts...it's hard to guess without hard data to look at.
Of course, it also raises the question of if you can do what want/need w/ textscan, why not use it instead of trying to beat sscanf into submission?
Mathieu NOE
2020년 11월 3일
hello
simple work around built on your code. They may be smarter way to do it, but ...
str = fileread('Document1.txt');
ind1 = findstr(str,'{');
ind2 = findstr(str,'}');
str1 = str(ind1:ind2);
mat = sscanf(str1,'{\r\r position %f %f %f\r gamma_angle %f\r weight %f\r collimator_setup %f %f %f %f %f %f %f %f\r\r}\r\r\r\r', [13,Inf]).';
답변 (2개)
Walter Roberson
2020년 11월 3일
You cannot do that with sscanf().
I recommend that instead you use regexp() with named tokens and the 'names' option . That will return a struct array in which the entries are character vectors. You can then do things like
gamma_angles = str2double({Parts.gamma_angle});
댓글 수: 1
Walter Roberson
2020년 11월 3일
편집: Walter Roberson
2020년 11월 3일
Also you can potentially use textscan with Header lines option. You can pass a character vector to textscan instead of a file identifier.
Stephen23
2020년 11월 3일
편집: Stephen23
2020년 11월 3일
"Is there a way to skip the first X lines in a text file when reading a text file using sscanf?"
No, because sscanf parses string/character vectors, not files. To efficiently parse files use fscanf:
"and was wondering if there's an easy way to do it for sscanf."
You can with one extra fscanf call to read (and discard) the file data up until the first "{" character (this relies on one condition: the "{" character must NOT exist anywhere in the"bla bla bla" text, but any other characters can).
fid = fopen('temp1.txt','rt');
fscanf(fid,'%*[^{]') % <- this answers your question.
fmt = '{\n\n position%f%f%f\n gamma_angle%f\n weight%f\n collimator_setup%f%f%f%f%f%f%f%f\n\n}\n';
mat = fscanf(fid, fmt, [13,Inf]).'
fclose(fid);
I created a sample file (attached, because you did not supply one) and tested the code, it imported everything correctly:
mat =
99.35000 56.73000 116.41000 90.00000 0.80000 16.00000 16.00000 16.00000 8.00000 16.00000 16.00000 16.00000 8.00000
99.36000 56.74000 116.42000 91.00000 0.80000 17.00000 17.00000 17.00000 9.00000 17.00000 15.00000 15.00000 7.00000
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!