skipping errors in a script

Hi, I have a script which consists of several functions:
functionA
functionB
functionC
Sometimes I get this error:
Index exceeds matrix dimensions.
Error in functionB (line 44) golrowA = cellContentsA((LA):(LA+120), :);
The question is how can I skip this error so that functionC gets executed because if the error occurs whilst running function B then the entire script stops and then the remaining functionC does not run.
thanks

 采纳的回答

One possibility is to check the length of ‘cellContentsA’ first:
cellContentsA = randi(10, 50, 10);
LA = 10;
if size(cellContentsA,1) < (LA+120)
fprintf(2,'\n\tSCREAMING LOUDLY THAT THE ROW SIZE OF ‘cellContentsA’ IS LESS THAN %.0f\n', LA+120)
else
golrowA = cellContentsA((LA):(LA+120), :);
end
What you choose to do after the warning and in the ‘else’ condition is up to you. This is simply one way of preventing the error, but since this leaves ‘golrowA’ unassigned, it may not solve your problem if ‘golrowA’ is needed later in your code.

更多回答(1 个)

Stephen23
Stephen23 2015-2-9
编辑:Stephen23 2015-2-9

2 个投票

In two words: try and catch.
Although the best solution is to figure out why you are getting those error messages, and fix the problem at the source. Because just skipping over errors is a great way to lose control of what the code is actually doing. Instead, dig into your code, find out why that line gets an index over the size of its array, create a special case to break out of the loop or whatever is a reasonable way to avoid this case. Then your code will be more robust, and it will not hide any future errors from you.
Don't just paste some wallpaper over the cracks in the wall, find out what the problem is and fix it.

类别

帮助中心File Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by