1

Is there a chance to set the header of the data file columns as label (not as key)?

I have data files with 5 or 6 columns and a header above each column. Now I would like to use the columnheader with the set label command. Is this possible?

Froop
  • 99
  • 8

1 Answers1

3

On a unixoid system, the head command helps:

header = system("head -n 1 ".filename)
label1 = word(header,1)
label2 = word(header,2)
...
set label 1 at 0.5,0.5 label1
set label 2 ....

MS win does not have the head command, you might use 'findstr /B \"#\"' instead, if the header line begins with a "#". Or use cygwin to get a full GNU + POSIX environment under Windows.

The word() function should split your header string at the same positions as columnhead(). Unless of course you have a different separator (not space or tab):

separator =","
p1 = strstrt(header,separator)
p2 = strstrt(header[p1+1:],separator)
...
label1=header[1:p1-1]
...
Karl
  • 2,117
  • 13
  • 26
  • Thank you for your answer. Today I had the chance to test it. But I get an error that says `The command "head" is either misspelled or was not found`. So I tried `header = system("< head -n 1".filename)`. This also gives an error: `The system can not find the specified file.` – Froop Sep 07 '15 at 08:18
  • With the "<", your system will try to feed a local file "head" into the variable, that can't work. You seem to not have the "head" program from the gnu core utilities. Have you tried running it on the system shell? (You are on a linux system?) – Karl Sep 07 '15 at 08:38
  • I have windows and I think the problem is that the `head` command is not available on the windows shell. So I tried to run my script with `wgnuplot` but there I get `popen failed`. So I have no idea what this means. – Froop Sep 07 '15 at 09:05
  • No, it's not available. If your header line begins with a "#" (and is the only line that does so), `"findstr /B \"#\" ".filename` would be the ticket. – Karl Sep 07 '15 at 13:48
  • So I have multiple lines beginning with "#" this will not work. But I managed to run gnuplot via cygwin and now I can use the `header = system("head -1 ".filename)` command. Thank you for your help! – Froop Sep 07 '15 at 14:01
  • Good thinking. ;-) I added that to my answer. – Karl Sep 08 '15 at 20:49
  • @Froop here is a platform-independent gnuplot-only solution: https://stackoverflow.com/a/72306388/7295599 there it is used for the title, but of course can also be used for a label. – theozh Jun 04 '22 at 07:58