3

I want to define gnuplot output which is a png file.how to define fileName on gnuplot.

    #This lines and also Traffic$j are define on my bash file.
    # Traffic$j is the name of file and that is valid. Traffic$j is on the loop
    # j is loop index
.
.

    fileName=Traffic$j
.
.

I try this:

gnuplot -e "filename=${!fileName}" plotFile

But I go this error:

line 0: constant expression required

I try ruakh's idea:

gnuplot -e "filename = '${!fileName}'" plotFile

But I gut this warning:

"plotFile", line 12: warning: Skipping data file with no valid points

line 12? look at my last line of the script.

How can I pass a variable to -e switch on gnuplot?

Update: My plotFile is this:

set terminal png size 720,450 enhanced font "H,11"
set output filename . '.png'

plot '../_numXY' using 2:3:(sprintf('%d', $1)) with labels offset 0,1 point pointtype 7 ps 2 lc rgb "green" notitle, \
filename using 1:2:($3-$1):($4-$2) with vectors linewidth 6 lc rgb "blue" notitle #line 12
1

2 Answers 2

1

The issue is not so much how to pass a variable, as how to quote a string. If, for example, $j is 3 and $Traffic3 is file.txt, then what you're passing to -e is filename=file.txt, when what you need to pass is something like filename = "file.txt" or filename = 'file.txt'. So:

gnuplot -e "filename = '${!fileName}'" plotFile

Edited to add: In a comment, you write:

thanks, $Traffic3 is not file.txt. or better to say I do not have $Traffic3 but I have Traffic3. Traffic3 is not parameter. It is the name of of file itself and not refer to another thing like file.txt

This means that you should not be writing ${!fileName}, but rather $fileName. The notation ${!fileName} means roughly, "O.K., so the value of $fileName is the name of another variable. Give me the value of that variable." So you just want:

gnuplot -e "filename = '$fileName'" plotFile
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, $Traffic3 is not file.txt. or better to say I do not have $Traffic3 but I have Traffic3. Traffic3 is not parameter. It is the name of of file itself and not refer to another thing like file.txt
@alex: In that case, you should not be writing ${!fileName} (which means "the value of the variable whose name is the value of $fileName -- for example, if $fileName is Traffic3, then ${!fileName} means $Traffic3), but rather $fileName. I'll update my answer.
0

You probably didn't want to cast indirect variable expansion?

gnuplot -e "filename=${fileName}" plotFile

3 Comments

thanks for help. But it tell me: line 0: undefined variable: Traffic1
Because that should be gnuplot -e "filename='${fileName}'" plotFile. Please see How to pass command line argument to gnuplot?.
@Christoph thanks you christoph. god bless you you make my day. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.