load function: how do you to skip lines in ASCII File that generate errors?

조회 수: 10 (최근 30일)
Peter
Peter 2022년 9월 24일
답변: Walter Roberson 2022년 9월 24일
I am using the load function to load data from an ASCII file filled with numeric data.
when running the load function, as in:
loaded_data=load(strcat(pat,'/',filename),' ');
If there is a line that has non-numeric values in it, the load function will not load the file.
for example, I will get an error like:
Unknown text on line number 468 of ASCII file /path/to/file/data_2022_09_18.dat
"********************".
where the string of *'s is in line 468 and creating the error.
Is there a way that I can load the file, and skip over lines which generate this error?
  댓글 수: 1
Stephen23
Stephen23 2022년 9월 24일
편집: Stephen23 2022년 9월 24일
Use READMATRIX and the options TreatAsMissing, TrimNonNumeric, etc. to ignore those characters.
Use READTABLE and the options TreatAsMissing, MissingRule, etc. to ignore those rows.

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

답변 (1개)

Walter Roberson
Walter Roberson 2022년 9월 24일
No. load() of a text file is documented as only being guaranteed to work for files saved with save -ascii
In practice load() of a text file will ignore lines whose first character is % but it is not able to ignore other forms of comments. You will need to switch to using a different way of loading text.
You might consider textscan with a 'CommentStyle' option.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by