\pgfforeachungrouped with three arguments

This is a follow-up to a previous question of mine, with a slight addition. I would like to loop over data in a file and give the plot styles as a list. In the old question I gave only a color and an index and Christian Hupfer came up with a working solution as did, with a slight variation, cmhughes.

Now, I would like to specify the style more precisely. For example, I would like to be able to draw dotted or dashed lines like in the following MWE

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsforeachungrouped \foridx/\forcol/\forstyle in {1/black/solid,2/red/dashed,3/green/dotted,4/blue/solid,5/orange/dotted}{
}
\end{axis}
\end{tikzpicture}
\end{document}


If I leave out \forstyle and the third arguments in the list, I get Christian Hupfer's solution which is working. Like this, it produces many errors:

! Undefined control sequence.
<argument> ...pgfplots@foreach@loc@TMPb \forstyle

l.9       }

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., \hobx'), type I' and the correct
spelling (e.g., I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

Missing character: There is no 1 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no b in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no c in font nullfont!
Missing character: There is no k in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no s in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no i in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no 2 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no r in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no s in font nullfont!
Missing character: There is no h in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no 3 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no g in font nullfont!
Missing character: There is no r in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no n in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no 4 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no b in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no u in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no s in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no i in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no 5 in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no r in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no n in font nullfont!
Missing character: There is no g in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no / in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no d in font nullfont!
! Undefined control sequence.
\pgfkeyscurrentkey ->\forstyle

l.9       }

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., \hobx'), type I' and the correct
spelling (e.g., I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.

NOTE: coordinate (2Y5.0e0],3Y0.0e0]) has been dropped because it is unbounded (

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y4.5833334e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y4.1666669e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y3.7500003e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y3.3333337e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y2.9166672e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y2.5000006e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y2.083334e0],3Y0.0e0]) has been dropped because it is unboun

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y1.6666675e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y1.2500009e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y8.333344e-1],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y4.166677e-1],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (2Y9.9e-7],3Y0.0e0]) has been dropped because it is unbounded

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y4.1666573e-1],3Y0.0e0]) has been dropped because it is unbo

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y8.3333243e-1],3Y0.0e0]) has been dropped because it is unbo

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y1.24999913e0],3Y0.0e0]) has been dropped because it is unbo

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y1.6666656e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y2.0833322e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y2.4999988e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y2.9166653e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y3.3333319e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y3.7499985e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y4.166665e0],3Y0.0e0]) has been dropped because it is unboun

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y4.5833316e0],3Y0.0e0]) has been dropped because it is unbou

! Package PGF Math Error: Could not parse input '' as a floating point number,
sorry. The unreadable part was near ''. (in 'x^').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9       }

(That was another \errmessage.)

NOTE: coordinate (1Y4.9999982e0],3Y0.0e0]) has been dropped because it is unbou


With the linked code snippet from cmhughes, I couldn't get it to work either with similar errors (probably the same problem)

So I tried to give the arguments to \addplot in a list:

\pgfplotsforeachungrouped \foridx/\forstyle in {1/{black,solid},2/{red,dashed},3/{green,dotted},4/{blue,solid},5/{orange,dotted}}{
}


This compiles but it produces only a solid black line (it loops only over the first element)

In fact, I could not get any example with three variables working. How do I use pgffor with three or more variables?

In PGFPlotsToDo list (v1.14) on page 35 it is written:

\pgfplotsforeachungrouped cannot be combined with three or more arguments like \foreach

So one has to find another solution to the problem at present. But luckily this is easily possible in this case by defining a custom cycle list.

For more details please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% create a cycle list that fits your needs ...
\pgfplotscreateplotcyclelist{mylist}{
{black,solid},
{red,dashed},
{green,dotted},
{blue,solid},
{orange,dotted}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=-100,
ymax=100,
legend pos=south east,
% ... and apply it here ...
% (or in \pgfplotsset' if it should be used as default for all plots)
cycle list name=mylist,
]
% ... then your loop simplifies to ...
\foreach \i in {1,...,5} {
% ... where I just added very thick' so the result can be
% seen easier
\addlegendentryexpanded{$x^\i$};