Menu

#1468 variable definition in "plot for" loop breaks with inline sampling range

None
closed-out-of-date
nobody
None
2017-03-18
2014-08-22
No

If an in-line sampling range is given within a "plot for []" loop, no definition can be added before the function to be plotted:

gnuplot> plot [0:10] for [i=0:9] [i:i+1] a=1 i+a w p
^
unexpected or unrecognized token

the parser seems to think "a=1" already is the function.

Discussion

  • Ethan Merritt

    Ethan Merritt - 2014-08-25

    The documentation was not quite right. It should say (and now does):

     Syntax:
           plot {<ranges>} <plot-element> {, <plot-element>, <plot-element>}
    
     Each plot element consists of a definition, a function, or a data source
     together with optional properties or modifiers:
           plot-element:
                {<iteration>}
                <definition> | {sampling-range} <function> | <data source>
                {axes <axes>} {<title-spec>}
                {with <style>}
    

    In other words, your plot command will work if it is written as
    plot [0:10] for [i=0:9] a=1 [i:i+1] i+a with points

    The sampling range must come immediately before the function it modifies.

     

    Last edit: Ethan Merritt 2014-08-25
  • Ethan Merritt

    Ethan Merritt - 2014-08-25
    • status: open --> open-works-for-me
     
  • Karl Ratzsch

    Karl Ratzsch - 2014-08-26

    Ah, OK. This however turns up a similar problem like the one we recently discussed on the newsgroup (function starting with bracket gets mistaken as function call in a previous definition):

    plot [0:10] for [i=0:9] a=i [i:i+1] x+a
    

    gives "internal error: substring range operator applied to non-STRING type". Parser holds the last pair of rectangular brackets to be a string operator on "i", one can get it to work by ending the definition clause with a number:

    plot [0:10] for [i=0:9] a=i*1 [i:i+1] x+a
    
     

    Last edit: Karl Ratzsch 2014-08-26
    • Karl Ratzsch

      Karl Ratzsch - 2014-08-29

      I guess this cannot be resolved without a major change in the synax, so we´ll just have to go with it?

      Patch adds an explanation to the help on "plot for []" (i reorganised that page a bit, too).

       
  • Ethan Merritt

    Ethan Merritt - 2014-08-29

    cannot be resolved ....

    I'm still not clear on what you are trying to achieve. Every example so far has been a case of adding a definition to the plot statement that is totally unnecessary, in the sense that it could just have well have been defined beforehand outside of the plot statment.

    Can you give an example that would actually require placing a definition in the plot statement itself? Otherwise I am inclined to say that the resolution of the problem is simply not to put definitions inside a plot statement, just as I also recommend not to put axis ranges in a plot statement. The syntax was originally designed to allow both of these, but IMHO that was a mistake.

     
    • Karl Ratzsch

      Karl Ratzsch - 2014-08-29

      Agreed, I think there is no "hard" need to do a definition inside the loop, one could always do it inside the function call, e.g. instead of

      plot for [i=1:10] a=af(i)  f(x)*a  title sprintf("%f",a)
      

      do

      plot for [i=0:10] f(x)*af(i) title sprintf("%f",af(i))
      

      It just (here) saves calling af(i) a second time.
      But as it is explicitly allowed, I thought it should be foolproof to use, or the possible hooks should at least be documented.

      However, the superior solution might indeed be to remove the possibility to make definitions inside the loop. At least that´d be an easier decision than removing inline axis ranges. They´re ugly, but have been used since forever and are just so damn convienient, lazywise. ;-)

       
  • Ethan Merritt

    Ethan Merritt - 2017-03-18
    • status: open-works-for-me --> closed-out-of-date
    • Group: -->
    • Priority: 5 -->
     

Log in to post a comment.