How to skip error and continue to execute the code

조회 수: 235 (최근 30일)
Jason
Jason 2016년 3월 9일
댓글: Jason 2016년 3월 11일
I have a for loop, but when I faced the following error, my script will stop executing. How can I do to skip the error and continue to execute the code?
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in ReadinData (line 160)
NonData=[Weight;Height;Age];
I have loop like this
for i = 1:100
How can I record i when the error happen, so I can check after the problem execution.
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2016년 3월 9일
Jason - wouldn't it be better to fix the error before continuing? The error message is telling you that the dimensions of your three variables are not compatible when trying to use the vertical concatenation. When this error occurs, what are the dimensions of Weight, Height, and Age?
Jason
Jason 2016년 3월 9일
Thanks. I have a lot of data to read, some data may have problem, I think it is the data problem, instead of code problem, so I want to skip the data

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

채택된 답변

Titus Edelhofer
Titus Edelhofer 2016년 3월 9일
Hi Jason,
one way would be to catch the error:
for i=1:100
try
NonData=[Weight;Height;Age];
% do something with your NonData
catch
fprintf('Inconsistent data in iteration %s, skipped.\n', i);
end
end
Titus
  댓글 수: 3
Titus Edelhofer
Titus Edelhofer 2016년 3월 10일
Sure, open a file
fid = fopen('output.err', 'wt');
and print to that file instead of to the screen
fprintf(fid, 'Inconsistent data in iteration %s, skipped.\n', i);
and close after the loop
fclose(fid);
Jason
Jason 2016년 3월 11일
Thanks. May I know why do not you use? Any reason save a .err file type and 'wt'?
fid = fopen('output.txt', 'w');

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

추가 답변 (1개)

Stalin Samuel
Stalin Samuel 2016년 3월 9일

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by