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

 Respuesta aceptada

Star Strider
Star Strider el 9 de Feb. de 2015

1 voto

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.

Más respuestas (1)

Stephen23
Stephen23 el 9 de Feb. de 2015
Editada: Stephen23 el 9 de Feb. de 2015

2 votos

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.

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Preguntada:

AA
el 9 de Feb. de 2015

Editada:

el 9 de Feb. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by