Shenan
Shenan

Reputation: 545

GNUplot input filename as variable

I want to input the filename to GNUplot as a variable. I've written a gnuplot script, which includes the following lines:

path="path/to/file"
plot "< cat $path | sed '1,4d'" using 1:2

When I run the script, the gnuplot window opens but there's nothing in it. If I replace $path with the actual path, the graph is correctly plotted.

Please can you suggest a way of doing this? Thank you.

Upvotes: 4

Views: 9231

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74705

You can do this to interpolate the value of the gnuplot variable in your command:

path="path/to/file"
plot "< cat ". path ." | sed '1,4d'" using 1:2

Note that it is not necessary to use cat and sed together:

path="path/to/file"
plot "<sed '1,4d' ". path using 1:2

Be careful with your spaces! You can use print instead of plot to see what command will be executed.

Sometimes, I like to use sprintf to do this kind of thing:

cmd = "1,4d"
plot sprintf("<sed '%s' %s", cmd, path) using 1:2

It's a bit longer but it's also clearer in my opinion.

By the way, in this particular case, you don't need to use any external tools at all. You could just do:

plot path using 1:2 every ::4

Which will skip the first 4 lines of your file.

Upvotes: 7

Related Questions