Found the solution. Will post here incase anyone comes across this later and wants the answer. It turns out it was a memory issue. The way I got around this was closing the figure in the loop, and then replotting in the next iteration.
So the plot commands were changed to:
plot(ax, t_A(1:j),k_A(1:j), 'Color', [94/153,60/153,153/153],'linewidth',3)
hold on
plot(ax, t_C(1:i), k_C(1:i), 'Color', [230/230,97/230,1/230],'linewidth',3)
And at the end of the loop I added the line to close the figure before starting the next iteration!
drawnow
frame = getframe(gcf);
writeVideo(writerObj,frame);
close(gcf);
end