I have a large number of ASCII files named in order of the form data.xxxx.tab, where "xxxx" is a number between 0000 and 9999. Each file contains 5 columns, where the first is for X-coordinate, second is for Y-coordinate and the remaining three are for variables which I wish to plot against X-coordinate. I need to know how to write a loop in gnuplot 4.6, that could plot consecutive graphs of one of the variables against X-coordinate.
I already tried the instructions given in the following posts:
Plotting with gnuplot from several files
and
gnuplot : plotting data from multiple input files in a single graph
but these created a single graph containing all the curves from all the data files together, whereas what I need are consecutive graphs that are plotted one after another, thus showing the evolution in time of the variable graph.
The following should work:
# fix axes for proper comparison between graphs
set xrange [0:10]
set yrange [0:10]
# if you want an animated gif
set term gif animate
set output 'output.gif'
# then plot your data
do for [n=0:9999]{
plot sprintf("data.%04d.tab", n) using 1:2 title 'case '.n
}
The %04d string inside the sprintf command prints the number n with until four zeros before the minimum field width of n, i.e. n=2 is printed as 0002, and n=9999 is printed as 9999.
I would suggest using a shell script that calls a gnuplot file
file plot.gp:
set term png
set out fname.".png"
set title fname
plot fname w l
and then in the shell:
for fname in data.????.tab; do gnuplot -e fname=\"$i\" plot.gp; done
you'll get a file named data.xxxx.tab.png file for each data.xxxx.tab.
Related
I am trying to use gnuplot to make a 'live' plot embedded in a C application, but I have encountered something that I want to change.
I am using gnuplot_i to 'interface' with gnuplot as if I were using gnuplot in the terminal.
Whenever I plot (two lines) I save the data to a file to then tell gnuplot to load the file and plot.
I would like to not have to save a data file and load the data directly into gnuplot. I do not know much of gnuplot so there may be a simple solution for what I want. What I would like is to have a command like this:
plot [1,2,3,4], [1,1,1,1], [1,2,3,4], [2,2,2,2]
|_____line 1_______| |______line 2______|
Is there a way to do this in gnuplot?
gnuplot> help datablock
There are two mechanisms for embedding data into a stream of gnuplot commands.
If the special filename '-' appears in a plot command, then the lines
immediately following the plot command are interpreted as inline data.
See `special-filenames`. Data provided in this way can only be used once, by
the plot command it follows.
The second mechanism defines a named data block as a here-document. The named
data is persistent and may be referred to by more than one plot command.
Example:
$Mydata << EOD
11 22 33 first line of data
44 55 66 second line of data
# comments work just as in a data file
77 88 99
EOD
stats $Mydata using 1:3
plot $Mydata using 1:3 with points, $Mydata using 1:2 with impulses
Data block names must begin with a $ character, which distinguishes them from
other types of persistent variables. The end-of-data delimiter (EOD in the
example) may be any sequence of alphanumeric characters.
I'm very new to matlab, or coding for that matter.
I'm running a simulation which outputs thousands of files. These files are .vtk and are read correctly by dlmread.
I tried reading one of them, defining it as a matrix and extracting column vectors out of this matrix. This works fine. What i need now is to not only read one of them, but all. The filenames vary by a number, for example cover1000.vtk, cover2000.vtk, ...., cover1200000.vtk.
I want all of them to be read with dlmread and stored as a different matrix. How do i do that? Here is what i have right now, working with one file at a time:
A_1000 = dlmread ('cover1000.vtk') %matrix a containing values from vtk file_in_loadpath
fx_1000 = A(1:20,1) %extracting vector with specific values
fx_ave_1000 = sum(fx_1000)/length(fx_1000) % average of the values in extracted vector
I'm thinking of a loop, but how do i create a loop with varying file names?
Also I've read that a loop is not the best idea, cell arrays would be better. But i have absolutely no idea how to implement any of this.
Thanks for your help!
cheers
You can use the function dir to list all the vtk files in your directory then loop over those files.
filename = dir('*.vtk'); %list all the vtk files in your current directory.
for ii = 1:length(filename)
A = dlmread (filename(ii).name) %matrix a containing values from vtk file_in_loadpath
fx{ii} = A(1:20,1) %extracting vector with specific value
fx_ave{ii} = sum(fx{ii})/length(fx{ii}) % average of the values in extracted vector
end
The results are now stored in two cells: fx and fx_ave.
I have a batch file for xmgrace. When I plot several sets of voltage signal measurements, the batch file Fourier-transforms them all and moves them to a different graph. I would like to add a command to the batch file, that will sum the Fourier-transformed plots of frequency. By that I mean that I want xmgrace to sum together the y values of all the data sets for each x value. All the data sets are the same length. Is there a way to do this?
Never mind, it was very easy. Added s0.y = s0.y + s1.y + s2.y + s3.y to my batch file. This sums all the y-values into one of the sets. I then added kill s1, kill s2, etc. to get rid of the others.
This question is related to Loop structure inside gnuplot? and the answer there by DarioP.
gnuplot 4.6 introduced the do command. How can I use this to loop over an array of for example files and colors? What is the correct syntax?
colors = "red green #0000FF"
files = "file1 file2 file3"
do for [i=1:3] {
plot files(i).".dat" lc colors(i)
}
If you want to have all files in a single plot, you need to use plot for[... (supported since version 4.4). Looping over several plot commands with do for (supported only since version 4.6) works only in multiplot mode.
The following two solutions both plot all data in one graph, but differ a bit in the iterations.
The first solution uses word to extract a word from a string directly when plotting.
colors = "red green #0000FF"
files = "file1 file2 file3"
plot for [i=1:words(files)] word(files, i).'.dat' lc rgb word(colors, i)
The second solution changes the linetype and then iterates directly over the word list instead of using an index.
colors = "red green #0000FF"
files = "file1 file2 file3"
set for [i=1:words(colors)] linetype i lc rgb word(colors, i)
plot for [file in files] file.'.dat'
Well, this is a continuation of my previous question. As, I mentioned, the data files are produced from a Fortran code. All the data files contain two columns of data. In the Fortran code I use the FORMAT (2(1X,D22.16)). So, the output files look like
-.4515533388641104D-01 -.6842652196656712D+01
-.1381692033642749D+00 0.6762844180244199D+01
-.5741668880663318D-01 -.7891086299010933D+01
-.1051164522902431D+00 0.7758389636011907D+01
-.7574000988697732D-01 -.8180315630079706D+01
-.7939204753736680D-01 0.8167097825331970D+01
-.1003250672387262D+00 -.7865995561517515D+01
-.6006135667296913D-01 0.7987393828927278D+01
..................... ......................
This is only a small sample portion of one data file. In order to plot this data file I use
plot "data001.out" u 1:2 w d lc rgb 'black'
However, gnuplot fails to read the data correctly and produced this plot
The correct plot, using Mathematica program, is this
I noticed, that if I change the FORMAT to (2(1X,F22.16)) (in decimal form) everything is OK. Why gnuplot cannot read data in exponential form? Is there a way to tell the program how to read this type of data?
From gnuplot 4.6 manual:
"Data may be written in exponential format with the exponent preceded by the letter e or E. The fortran exponential specifiers d, D, q, and Q may also be used if the command set datafile fortran is in effect."
So you should put set datafile fortran before plot.
Hope it helps!