For the code you linked, something like this should work:
# Instead of lines, sc, txt = nothing, nothing, nothing
# define a closure over that state
plotter = let lines = nothing, sc = nothing, txt = nothing # Note: Must all be on same line as let!
function(x, y; kwargs...)
lines, sc, txt = plot2d(x, y; lines, sc, txt, kwargs...)
end
end
# ...
# Now in the loop call plotter instead of plot2d
if mod(i, 5) == 0
plotter(kps4.pos, reltime; zoom=ZOOM, front=FRONT_VIEW,
segments=set.segments, fig="side_view")
end
This “let-over-lambda” construction is a general idiom for creating stateful closures and broadly applicable, i.e., I did not check what plot2d actually does.