textscan question skip lines

I'm using textscan to import a non rectangular data text file. I'm having an issue skipping lines. The file has 10 rows at the begining which I'd like to skip, and for that I use the HeaderLines option.
fid = fopen('test.txt');
c = textscan(fid,'%s%d %d %d %d %d %d','HeaderLines', 10 );
fclose(fid);
The rest of the text file periodically, every 50 lines has a few lines that I'd also like to skip, this is where I'm running into a problem. Any ideas?

답변 (2개)

Walter Roberson
Walter Roberson 2013년 1월 29일

1 개 추천

When you textscan, add a count after the format. The count will be the number of times the format is re-applied, and thus limits the number of lines read with that textscan() call. You would then loop around doing such calls and splicing the data together.
In some cases you can make use of the CommentStyle parameter to cause textscan() to automatically skip parts of the input. For example, if each of the lines to be skipped started with ';' you could set CommentStyle to ';' and the lines would be skipped. If this case applies to you, you would not need to put a count on or splice the data together.

댓글 수: 3

Harishkumar Gajakosh
Harishkumar Gajakosh 2022년 4월 8일
편집: Harishkumar Gajakosh 2022년 4월 8일
Lets suppose a case where i need to skip the lines starting from '%'. Then, How do we do that? I tried to do the following.
data = textscan(simID,'N%d G%d X%f Y%f Z%f','Delimiter',' ','CommentStyle','(','% ');
But when i run it i get the error as
Error using textscan
Name-value pair arguments must come in pairs.
Error in TootPath_Simulator (line 2)
data = textscan(simID,'N%d G%d X%f Y%f Z%f','Delimiter',' ','CommentStyle','(','% ');
'CommentStyle',{'(','% '})
% ^ ^
Harishkumar Gajakosh
Harishkumar Gajakosh 2022년 4월 9일
편집: Harishkumar Gajakosh 2022년 4월 9일
@Stephen: I think that if i were to use this then it would read everything between '(' and '%' as comment i.e the comment starts at '(' and ends at '%', based on the reference given here, under 'CommentStyle — Symbols designating text to ignore' section.
Thus, it is not helping. I appreciate the effort though. Thanks.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

질문:

2013년 1월 28일

편집:

2022년 4월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by