Q:

Plotting multiple data series with multiple styles in a gnuplot for loop

I'm trying to generate some plots with gnuplot, with both lines and error bars, but I'm running into some trouble.  When I run this code (with no prior definition of i, but a prior definition of m = 9):
plot for [i=2:5] \
'../Data/Clustering/c_vs_n_with_error_'.i.'ball_m'.m.'.txt' u 1:2 w linespoints t ''.i.'-Ball', \
'../Data/Clustering/c_vs_n_with_error_'.i.'ball_m'.m.'.txt' u 1:2:3 w yerrorbars t ''

I get the following error:
warning: Skipping unreadable file "../Data/Clustering/c_vs_n_with_error_6ball_m9.txt"

And there is, in fact, no file of that name.
My issue is that the resulting plot does not have error bars (it's plotted in the lines-only style); it's as if the second use of the same file is simply ignored.  However, when I change the iteration range to [i=2:4], I receive no error - and the data range for the i=5 is plotted with error bars instead of lines.
I am plotting this way because other information I explored told me that it was not possible to plot with multiple styles (linespoints and yerrorbars) simultaneously, and that this was a good solution (but it gave examples only for a single data series, without a for loop).
Two things I would like to avoid doing are a) writing out all the files (without using iteration) and b) resorting to a shell script, because I'd much rather prefer to understand gnuplot than work around it.
Thank you for your help!

A:

The scope of an iteration ends at the next comma or the end of the command, whichever comes first. Compare e.g the following two examples (from the gnuplot manual):
plot for [i=1:3] j=i, sin(j*x)

and
plot for [i=1:3] j=i sin(j*x)

Accordingly, you can change your plot command to:
file(n) = sprintf('../Data/Clustering/c_vs_n_with_error_%dball_m'.m.'.txt', n)
plot for [i=2:5] file(i) u 1:2 w linespoints t ''.i.'-Ball', \
     for [i=2:5] file(i) u 1:2:3 w yerrorbars t ''

