简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM