nox
nox

Reputation: 252

gnuplot - how to convert read time format to double/float/use it as function parameter

I've got a problem with read-in time values in gnuplot. I have a data file

#data.dat
00:50.254
00:50.435
00:51.356
00:53.245
00:54.730
...

with format '%M:%S'. So I'm reading it in with my gnuplot file:

set xdata time
set timefmt "%M:%S"
set xtics format "%M:%S"

set grid
set xrange [ 0:"01:30" ]
set yrange [0:10]
set xtics "00:00",10,"01:30"
set mxtics 2
set ytics 0,2,10
set mytics 2

p 'data.dat' u 1:1

Here I want to plot the data really 1:1 as a test. The result is: I will get just the value of '%M' as y-value. This can be corrected by using set ydata time. But I want to do some calculation with the time data and need the seconds (or seconds including minutes, sth. like %M*60+%S: 01:30->90). I want to pass it to a function, or in my special case: summing it up with smooth freq after some calculation in a function:

bin(x) = floor(x+0.5)
p 'data.dat' u (bin($1)):(1.0) smooth freq w boxes

How can I access the %S part of the time?
How can I convert a read-in time to a double/float?

Thank you for your help in advance!

edit: I got an idea right after posting: is the read-in data an array and I can read out this array? I'll try it out.

edit2: internal error : non-STRING argument... This did not work
In my special case, I could maybe use : as delimiter and just read out the mins and secs as two colums... I'll give it a try too. But this wont help, if there are more columns with other data that needs to be plotted...

edit3: Even setting the delimiter (separator) doesn't solve the problem yet, but an error message occurs: Bad format character at my plot line.
Here my new gnuplot command file:

set datafile separator ':'

set timefmt "%M:%S"
set xtics format "%M:%S"

set grid
#set xrange [ 0:"01:30" ]
set xrange [0:90]
set yrange [0:92]
#set xtics "00:00",10,"01:30"
set xtics 0,10,90
set mxtics 2
set ytics 0,2,10
set mytics 2

p 'data.dat' #u 1:2 #($1*60+$2):($2)

Upvotes: 2

Views: 2895

Answers (1)

Matthew
Matthew

Reputation: 7590

Internally, gnuplot stores the time as the number of seconds since some epoch (the epoch is different between versions, but is the Unix epoch in the 5.0 line).

Normally, using the time format settings, gnuplot handles conversions itself. However, there are functions that can be used for processing the times manually as well, for example

strptime("%M:%S","50.254")

will process the time of your first line into a number of seconds. We can even use this in plotting commands, as long as we are careful to read columns as strings

plot "data.dat" u 0:(strptime("%M:%S",strcol(1)))

will plot your data file as seconds elapsed versus the line number. As you are not specifying date info in your time format, gnuplot defaults to the beginning of it's first day in the epoch, so we just get 50 some seconds (otherwise the numbers could be much larger).

enter image description here

In general, you can handle any time format like this. It may be a little difficult if you have spaces or such in your time format, as you won't be able to rely on gnuplot's ability to know how much to "chunk" into the column without setting a time format (but this can be handled by using a different separator or quoting times).

See help strptime for the function which converts the time format to the internal representation and help strftime for the function which converts one of these internal representations back to a date string.

Upvotes: 4

Related Questions