gnuplot store one number from data file into variable

前端 未结 3 425
北恋
北恋 2020-12-01 12:51

OSX v10.6.8 and Gnuplot v4.4

I have a data file with 8 columns. I would like to take the first value from the 6th column and make it the title. Here\'s what I have

相关标签:
3条回答
  • 2020-12-01 13:36

    Here is a less 'awk'-ward solution which assigns the value from the first row and 6th column of the file 'Data.txt' to the variable x16.

    set table
    # Syntax: u 0:($0==RowIndex?(VariableName=$ColumnIndex):$ColumnIndex)
    # RowIndex starts with 0, ColumnIndex starts with 1
    # 'u' is an abbreviation for the 'using' modifier 
    plot 'Data.txt' u 0:($0==0?(x16=$6):$6)
    unset table
    

    A more general example for storing several values is given below:

    # Load data from file to variable
    # Gnuplot can only access the data via the "plot" command
    set table
    # Syntax: u 0:($0==RowIndex?(VariableName=$ColumnIndex):$ColumnIndex)
    # RowIndex starts with 0, ColumnIndex starts with 1
    # 'u' is an abbreviation for the 'using' modifier 
    # Example: Assign all values according to: xij = Data33[i,j]; i,j = 1,2,3
    plot 'Data33.txt' u 0:($0==0?(x11=$1):$1),\
                    '' u 0:($0==0?(x12=$2):$2),\
                    '' u 0:($0==0?(x13=$3):$3),\
                    '' u 0:($0==1?(x21=$1):$1),\
                    '' u 0:($0==1?(x22=$2):$2),\
                    '' u 0:($0==1?(x23=$3):$3),\
                    '' u 0:($0==2?(x31=$1):$1),\
                    '' u 0:($0==2?(x32=$2):$2),\
                    '' u 0:($0==2?(x33=$3):$3)
    unset table
    print x11, x12, x13     # Data from first row
    print x21, x22, x23     # Data from second row
    print x31, x32, x33     # Data from third row
    
    0 讨论(0)
  • 2020-12-01 13:38

    This is a very old question, but here's a nice way to get access to a single value anywhere in your data file and save it as a gnuplot-accessible variable:

    set term unknown #This terminal will not attempt to plot anything
    plot 'myfile.dat' index 0 every 1:1:0:0:0:0 u (var=$1):1
    

    The index number allows you to address a particular dataset (separated by two carriage returns), while every allows you to specify a particular line.

    The colon-separated numbers after every should be of the form 1:1:<line_number>:<block_number>:<line_number>:<block_number>, where the line number is the line with the the block (starting from 0), and the block number is the number of the block (separated by a single carriage return, again starting from 0). The first and second numbers say plot every 1 lines and every one data block, and the third and fourth say start from line <line_number> and block <block_number>. The fifth and sixth say where to stop. This allows you to select a single line anywhere in your data file.

    The last part of the plot command assigns the value in a particular column (in this case, column 1) to your variable (var). There needs to be two values to a plot command, so I chose column 1 to plot against my variable assignment statement.

    0 讨论(0)
  • 2020-12-01 13:39

    You have a few options...

    FIRST OPTION:

    use columnheader

    plot file using 1:3 title columnheader(6)
    

    I haven't tested it, but this may prevent the first row from actually being plotted.

    SECOND OPTION:

    use an external utility to get the title:

    TITLE="`head -1 datafile | awk '{print $6}'`"
    plot 'datafile' using 1:3 title TITLE
    

    If the variable is numeric, and you want to reformat it, in gnuplot, you can cast strings to a numeric type (integer/float) by adding 0 to them (e.g).

    print "36.5"+0
    

    Then you can format it with sprintf or gprintf as you're already doing.

    It's weird that there is no float function. (int will work if you want to cast to an integer).

    EDIT

    The script below worked for me (when I pasted your example data into a file called "datafile"):

    K = "`head -1 datafile | awk '{print $6}'`"
    K=K+0  #Cast K to a floating point number
    graph(n) = sprintf("K=%.2e",n)
    plot "datafile" using 1:3 title graph(K)
    

    EDIT 2 (addresses comments below)

    To expand a variable in backtics, you'll need macros:

    set macro
    file="mydatafile.txt"
    #THE ORDER OF QUOTES (' and ") IS CRUCIAL HERE.
    cmd='"`head -1 ' . file . ' | awk ''{print $6}''`"'
    # . is string concatenation.  (this string has 3 pieces)
    # to get a single quote inside a single quoted string
    #   you need to double.  e.g. 'a''b' yields the string a'b 
    data=@cmd
    

    To address your question 2, it is a good idea to familiarize yourself with shell utilities -- sed and awk can both do it. I'll show a combination of head/tail:

    cmd='"`head -2 ' . file . ' | tail -1 | awk ''{print $6}''`"'
    

    should work.

    EDIT 3

    I recently learned that in gnuplot, system is a function as well as a command. To do the above without all the backtic gymnastics,

    data=system("head -1 " . file . " | awk '{print $6}'")
    

    Wow, much better.

    0 讨论(0)
提交回复
热议问题