Search code examples
parsinggnuplot

Removing the file paths and using the file number to perform some calculations while plotting


I am trying to read .txt files from a directory which have the following order;

x-23.txt
x-43.txt 
x-83.txt
:
:
x-243.txt

I am calling these files using filename = system("ls ../Data/*.txt"). The goal is to load these files and plot certain columns. At the same time, I am trying to parse the file names such that it would look like as below so that I can use them as title in the plot and add/subtract them from a certain column;

23
43
83
:
:
243

For that, I tried the following;

dirname = '../Data/'
str = system('echo "'.dirname. '" | perl -pe ''s/x[\d-](\d+).txt/\1.\2/'' ') 
cv = word(str, 1)

The above lines doesn't seem to trim and produce numbers on the files. The code all together;

filelist1 = system("ls ../Data/*.txt")
print filelist1
dirname = '../Data/'
str = system('echo "'.dirname. '" | perl -pe ''s/x[\d-](\d+).txt/\1.\2/'' ') 
cv = word(str, 1)
plot for [filename1 in filelist1] filename1 using (-cv/1000+ Tx($4)):(X($3)) with points pt 7 lc 6 title system('basename '.filename1),\

I am trying to use the file numbers "cv" after parsing the .txt files to subtract them from column Tx($4) while plotting.


Solution

  • directory = "../temp/"
    filelist = system("cd ../temp/ ; ls *.txt")
    files = words(filelist)
    filename(i) = directory . word(filelist,i)
    title(i) = word(filelist,i)[3 : strstrt(word(filelist,i),'.')-1]
    
    plot for [i=1:files] filename(i) using ... title title(i)
    

    Test case (edited to show pulling files from another directory):

    gnuplot> print filelist
    x-234.txt
    x-23.txt
    x-2.txt
    x-34.txt
    
    gnuplot> do for [i=1:files] { print i, ": ", filename(i) }
    1: ../temp/x-234.txt
    2: ../temp/x-23.txt
    3: ../temp/x-2.txt
    4: ../temp/x-34.txt
    
    gnuplot> plot for [i=1:files] x*i title title(i)
    

    enter image description here