sscanf how to skip lines - not textscan!!!
12 ビュー (過去 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!