2

Is there a way for Gnuplot to read and recognize structured strings? Specifically, I have a few hundred files, all containing measurement data, with measurement conditions defined in the filename.

My files look something like "100d5mK2d0T.txt", which would mean that this data was acquired at 100.5mK temperature and 2.0T magnetic field.

Any chance I could extract the temperature and field strength data from the name, and use them as labels in the plot?

Thanks in advance.

2
  • How are you running your gnuplot script--interactively or calling a canned script repeatedly? Also, what OS/gnuplot version are you using? Commented Apr 4, 2014 at 4:08
  • I would suggest to use external parser. Gnuplot has the c-like sprintf function but not the scanf. Nevertheless some string manipulation is possible. As example name="100d5mK2d0T.txt"; temp = int(name[0:3])+int(name[5:6])/10.0; Bfield = int(name[8:9])+int(name[10:11])/10.0 will do what you want, but this implies the formatting is strictly fixed... It would'n work for strings like 99d5mK2d05T.txt Commented Apr 4, 2014 at 6:45

1 Answer 1

2

With gnuplot's internal string processing you could come up with a solution (using substr and strstrt), but thats quite verbose.

Its better to use an external tool for the string processing, like perl:

filename = '100d5mK2d0T.txt'
str = system('echo "'.filename. '" | perl -pe ''s/(\d+)d(\d+)mK(\d+)d(\d+)T.txt/\1.\2 \3.\4/'' ')
temperature = word(str, 1)
magnetic_field = word(str, 2)

set label at graph 0.1,0.9 "Temperature: ".temperature." mK"
set label at graph 0.1,0.8 "Magnetic field: ".magnetic_field." T"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this is exactly what I needed! One more detail, if you don't mind. Besides the useful information, filenames also carry some junk at the end. Something like "49d0mKnow_50d0mKset_3d00T_D03_1_240_001.dat". So, I would like to extract the second temperature and the magnetic field, while ignoring everything else. I managed to do so by modifying your code a bit: perl -pe ''s/(\d+)d(\d+)mKnow_(\d+)d(\d+)mKset_(\d+)d(\d+)T(\s)*/\1.\2 \3.\4 \5.\6 \7/'' I'm not really familiar with Perl syntax, so I just wanted to ask if this is okay, or is there a better way to do it?
I'm also not very familier with perl, just with regular expressions. As long as it works, its fine :) Depending on the different formats which your file names can have, it may be easier to use one perl call for each variable instead of extracting all together and then splitting them with word inside gnuplot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.