Try something like this:
x = linspace(0, 5*pi, 250);
limit_y = @(y,Capacity) (y<0).*0 + (y>Capacity).*Capacity + ((y>=0) & (y<Capacity)).*y;
plot(x, y, 'LineWidth',1)
plot(x, limit_y(y,Capacity), '--', 'LineWidth',1.5)
Note that no explicit loops are necessary, since the anonymous functrion does everything in one call.
The anonymous function is designed to work on vectors, however it is certainly possible to call the anonymous function in a loop with individual scalar values as the ‘y’ argument. It will work the same way, regardless.
Experiment with it to get the result you want.
EDIT — (17 Apr 2021 at 17:12)
Added plot image to illustrate function effect —