Hi I just came across the same problem. I run a simulation with different parameters and for some parameters it takes very long time. Sometimes it's just a numerical instability.
To stop the function by a timeout I use events . This example will stop the calculation after 5 seconds and you will get what the solver could calculate during this time.
function Results = RunCalculation(Parameters)
xoverFcn = @(T, Y) MyEventFunction(T, Y);
options = odeset('Events',xoverFcn);
[T,Y] = ode45(NPZModel, TSpan, [1, 1, 1], options);
function [VALUE, ISTERMINAL, DIRECTION] = MyEventFunction(T, Y)
TimeOut = 5;
VALUE = toc-TimeOut
ISTERMINAL = 1;
DIRECTION = 0;