gnuplot

How to replace macros with functions in gnuplot?


How can the following, increasingly complicated macro stuff be replaced by a function? Calling macros within macros is quite error-prone

sel_col = 'column(selected_column)'
ref_col = 'column(reference_column)'
volume = '( (@sel_col) - grad * ( @ref_col - CM_mean ) )'
plot FILE using (@volume): ...

Should become something like this:

volume(s,r) = ...
plot FILE (volume(selected_column,reference_column)): ...

Solution

  • If possible I would avoid macros unless there is no other way. As I understand you want to create a function with columnheaders as input parameters to calculate some values.

    In gnuplot you can either give column numbers, e.g. using 1:2:3 or using (column(1)):(column(2)):(column(3)) or shorter using ($1):($2):($3) or variables which contain numbers, e.g. for a=1, b=2, c=3 then you can write using a:b:c.

    Alternatively, you can use columnheaders using "length":"width":"height" or variables, e.g. L="length", W="width", H="height" and then write using L:W:H or using (column(L)):(column(W)):(column(H)).

    So, probably you are looking for something like:

    Script:

    ### functions with headercolumns as parameters
    reset session
    
    $Data <<EOD
    length  width   height
    11      12      13
    21      22      23
    31      32      33
    EOD
    
    volume(l,w,h) = column(l) * column(w) * column(h)
    
    l = "length"
    w = "width"
    h = "height"
    
    set table $Test
        plot $Data u l:w:h:(volume(l,w,h)) w table
    unset table
    print $Test
    ### end of script
    

    Result:

     11      12      13      1716
     21      22      23      10626
     31      32      33      32736