I'm reading in a .csv file with a large header and one undesired line at the very end of the file. I'm using textscan and the parameter 'headerlines' to successfully skip the header, but I can't figure out how to skip the last line of the file.
I'm having no problems with skipping lines at the beginning of the file, only with skipping the last line. Could anyone offer a detailed description of how to skip the last line of the file?

 채택된 답변

Jan
Jan 2012년 5월 17일

0 개 추천

You cannot ignore the trailing line using textscan(). I recommend to import all data and delete the last row afterwards. I assume something like this will help:
C = textscan(...);
for iC = 1:length(C)
C{iC} = C{iC}(1:end, :);
end

댓글 수: 1

I removed the last line with a slight change. I used:
C{iC} = C{iC}(1:end-1, :);

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 5월 17일

0 개 추천

The only way textscan() has to do that is if you provide a count of the number of times you want the format to be applied. This means you would have to know the exact number of lines in the file.
If you do not know the number of lines in the file, then unfortunately MATLAB does not provide any routine that can tell you. You would need to read the file and count the lines.
Kees Pruis
Kees Pruis 2013년 4월 15일

0 개 추천

A slow but maybe helpfull solution might be the use of fgetl. To be able to use this you need to know what's on the last line which you want to skip. Your code would than be similar to the example below.
while ~feof(fid) && ~strcmp(fgetl(fid),'End of the file') D = textscan(fid); end

카테고리

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

태그

질문:

2012년 5월 17일

댓글:

2018년 11월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by