gnuplot

How can gnuplot select certain named columns within a for loop?


The following script has some strange problems. When I load it after starting gnuplot, I get the following error:

gnuplot> load "test_plot_data.gp"
         "test_plot_data.gp" line 46: warning: selected_columns is not a string variable
"lh_01":"rh_01"
         "test_plot_data.gp" line 62: undefined variable: with

gnuplot> 

When I load it again, it correctly selects the volumes from the specified columns “lh_01”:“rh_01” until it reaches “pause -1” in the loop. When it continues, the column names “lh_02”:“rh_02” are updated correctly, but it still outputs the values from the previous run (“lh_01”:“rh_01”).

When I load this script afterwards in the same Gnuplot session, I always get the values of “lh_02”:"rh_02", although the column names are always set correctly. Do I need to make some special settings to avoid this behaviour?

reset

set datafile missing "NaN"
set datafile columnheader

# Set up for subjects and column usage
$Data << EOD
# TP1
gp   HS   Age     lh_01    rh_01    lh_02  rh_02
2    0    37.74   7030.26  7353.44  70.26  73.44
2    0    26.42   7652.64  8566.62  76.64  85.62
2    0    30.20   6938.74  7489.41  69.74  74.41
2    0    27.68   6619.97  6224.74  66.97  62.74
2    1    18.87   7472.23  8635.48  74.23  86.48
2    0    34.56   6612.52  7347.70  66.52  73.70
2    0    33.54   7049.64  7137.09  70.64  71.09
3    1    21.47   8327.11  8745.67  83.11  87.67
1    0    22.00   7924.66  8694.33  79.66  86.33
1    0    24.96   7605.58  8072.01  76.58  80.01
3    1    12.10   8883.32  9230.87  88.32  92.87
2    0    24.72   8271.73  8465.82  82.73  84.82


# TP 2
gp   HS   Age     lh_01    rh_01    lh_02   rh_02
2    0    38.84   7069.54  7380.43  70.54   73.43
2    0    33.14   7705.56  8076.87  77.56   80.87
2    0    39.78   7346.33  7712.80  73.33   77.80
2    0    28.98   6963.54  7171.83  69.54   71.83
2    1    23.71   6830.13  8113.97  68.13   81.97
2    0    39.93   7438.96  7020.43  74.96   70.43
2    0    44.92   6943.02  7204.57  69.02   72.57
3    1    26.82   8245.33  8335.07  82.33   83.07
1    0    24.93   7396.14  7853.01  73.14   78.01
1    0    29.79   7421.25  7810.83  74.25   78.83
3    1    14.55   8672.95  8937.76  86.95   89.76
2    0    27.30   8668.1   8506.81  86.1    85.81
EOD

LABELS = "lh_01 rh_01 lh_02 rh_02"

# Search string for hemispheres (adjust based on your data)
search_string = 'lh'

# Loop through selected labels and group labels
do for [selected_label in LABELS] {
    if ( strstrt( selected_label, search_string ) > 0 ) {
        # Strip hemisphere label
        selected_label = substr( selected_label, strlen( search_string ) + 1, strlen( selected_label ) )

        # Set column names
        left = "lh".selected_label
        right = "rh".selected_label

        # Prepare selected columns for plotting
        selected_columns = sprintf("\"%s\":\"%s\"", left, right)
        print selected_columns

        # Store temporary table
        set table $Temp
            plot $Data using @selected_columns with table
        unset table

        print $Temp

        pause -1
    }
}

### End of script

Solution

  • It's not clear to me why you are taking a detour via sprintf() and @ macro. Simply take the string variables left and right into your plot command, which is plotting the columns with that headers.

    plot $Data u left:right with table