How to skip error and continue to execute the code

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 Comments

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?
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

Sign in to comment.

 Accepted Answer

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 Comments

If I do not want to just display
fprintf('Inconsistent data in iteration %s, skipped.\n', i);
Can I write this skip information in a text file?
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);
Thanks. May I know why do not you use? Any reason save a .err file type and 'wt'?
fid = fopen('output.txt', 'w');

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 9 Mar 2016

Commented:

on 11 Mar 2016

Community Treasure Hunt

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

Start Hunting!