3

I want to plot the data stored in bunch of files using gnuplot. If the files were named using sequential numbers, (eg. "1.dat" "2.dat", ...) I'd use something like

plot for [i=1:10] i.'.dat' u 1:2 w lp t 'I='.i;

However, the files are now named using powers of 2, i.e. "2.dat", "4.dat", "8.dat", .... I tried

plot for [i=1:10] (2**i).'.dat' u 1:2 w lp t 'I='.(2**i);

but I get the error

STRING operator applied to non-STRING type

I suppose this happens because gnuplot considers (2**i) as a floating point number rather than integer.

I'm sure there is a way to do what I want to do but as I'm very new to using the control statements of gnuplot I cannot find out how. Could someone please help me?

choroba
  • 231,213
  • 25
  • 204
  • 289
MikeL
  • 2,369
  • 2
  • 24
  • 38

2 Answers2

3

You can use sprintf to convert a number to a string:

plot for [i=1:10] sprintf('%d',2**i).'.dat' u 1:2 w lp t 'I='.(2**i)

Interestingly, concatenating (2**i) with 'I=' in the title causes no problems.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • Integers are promoted to strings when operated on by the string concatenation `.` operator, which is why the `'I='.(2**i)` part works. There is no such operator on an integer though. If you're going to use `sprintf` you may as well do `sprintf('%d.dat',2**i)` to save a step. – Tom Fenech Aug 13 '14 at 21:21
3

Try using an empty string ("") to commence the string concatenation operation. That is "".(2**i).".dat" instead of (2**i).".dat".

Miguel
  • 7,497
  • 2
  • 27
  • 46
  • 1
    First I thought, that one wouldn't need this kind of construct. And I would also prefer using `sprintf`. But right now I found a case, where using your solution is the only way (see [How to change the time format based on its data](http://stackoverflow.com/a/25304926/2604213)). Using `set for [y=2000:2014] xtics add (sprintf('%d/01/01', y) sprintf('%d-01-01 00:00', y))` doesn't work, whereas using `''.y.'-01-01 00:00'` for the second string does work! So it seems like here the time strings are parsed only if they are real string, and not results from a function call. +1 :) Maybe that is a bug? – Christoph Aug 14 '14 at 09:51
  • @Christoph Hum, no idea... I've been trying different options for the example you put here but to no avail... – Miguel Aug 14 '14 at 11:23