Reading external data files in gnuplot - file

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!

Related

Interacting with gnuplot from a C program. How to type in an array to plot

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.

Prolog - load 2D array from text file into program

I'm currently working on a Prolog program which would, logically, have some kind of "save/load" feature. I've gotten the save part to work, where I'm now (as a start) creating three *.txt files, which will contain a 2D array/list each. However, I'm facing some issues trying to load it back into the program.
What I have right now is something as simple as:
% Initialize globals
?- nb_setval(aisles_global, []).
% Load all previously saved data from the given .txt files
load_all():-
exists_file('C:\\Users\\Xariez\\Desktop\\aisles.txt'),
open('C:\\Users\\Xariez\\Desktop\\aisles.txt', read, InAisles),
read_line_to_codes(InAisles, AisleString),
% read_line_to_string(InAisles, AisleString),
writeln(AisleString),
nb_setval(aisles_global, AisleString),
close(InAisles).
As previously mentioned, the files will have a 2D array each, but as an example:
aisles.txt
[["Beer", "Cider" ], [ "Milk", "Juice" ], ["Light Bread", "Dark Bread"]]
I've tried using both read_line_to_codes/2 and read_line_to_string/2. While it technically works when reading it into codes, I feel like it would quickly become annoying to reconstruct a 2D list/array since it's now got every character as a code. And while reading into a string succeeds in the reading part, we now have a string that LOOKS like a list, but isn't really one (if I've understood this situation correctly?). And hence I'm here.
If anyone got any ideas/help, that'd be greatly appreciated. Thanks!
Prolog has predicates for doing input/output of terms directly. You don't need to roll these yourself. Reading terms is done using read, while for writing there are several options.
Your best shot for writing is probably write_canonical, which will write terms in "canonical" syntax. This means that everything is quoted as needed (for example, an atom 'A' will be printed as 'A' and not as plain A like write would print it), and terms with operators are printed in prefix syntax, which means you get the same term even if the reader doesn't have the same operators declared (for example, x is y is printed as is(x, y)).
So you can write your output like:
dump(Aisles, Filename) :-
open(Filename, write, OutAisles),
write_canonical(OutAisles, Aisles),
write(OutAisles, '.'),
close(OutAisles).
Writing the . is necessary because read expects to read a term terminated by a period. Your reading predicate could be:
load(Aisles, Filename) :-
open(Filename, read, InAisles),
read(InAisles, Aisles),
close(InAisles).
Running this using some example data:
?- aisles(As), dump(As, aisles).
As = [["Beer", "Cider"], x is y, 'A', _G1380, ["Milk", "Juice"], ["Light Bread", "Dark Bread"]].
?- load(As, aisles).
As = [["Beer", "Cider"], x is y, 'A', _G1338, ["Milk", "Juice"], ["Light Bread", "Dark Bread"]].
The contents of the file, as you can check in a text editor, is:
[["Beer","Cider"],is(x,y),'A',_,["Milk","Juice"],["Light Bread","Dark Bread"]].
Note the canonical syntax for is. You should almost certainly avoid writing variables, but this shouldn't be a problem in your case.

How to plot several datafiles in consecutive graphs using gnuplot

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.

Is it possible to use Sage to compute results from data

I have set of data point (x_i,y_i) from a text file. How can I write a C-program that reads those data, send the data to Sage, computes the Pearson correlation and send the result back to C. I have no idea how can I use C to give input to some Linux-program and read its output to a variable.
OK, let me get it straight: you are working on a C program, and within that program you need to calculate Pearson's correlation coefficient. You'd like to pass these calculations to Sage rather than code them yourself.
Now, I don't know Sage, but I guess it is possible to run it from command line. Assuming that you can prepare an input file or files for Sage, and run the calculations in Sage producing an output, I would then use the system from stdlib.h (man 3 system) to call the command line. Here is the outline of the steps in your C program:
prepare temporary files for Sage's input and output
construct the command line of Sage
use system() to run the command line
parse the temporary file name where Sage stored it's output.
That said, I would not do it using Sage. Pearson correlation coefficient is easy enough to implement in C, and if you do it, your program will not depend on the whole Sage installation.

Writing a list of data values to a .dat file to plot in GNUplot (in C)

Hiya, I have written two different methods to numerically differentiate a function and I am looking for a way to compare them. I have installed GNUPlot and would like to make a file (e.g. approximations.dat) for it to plot. At the moment my programn prints a series of columns with x-coordinate, approximation 1, approximation 2 and actual value like this:
x-coord approx 1 approx 2 actual
x-coord approx 1 approx 2 actual
x-coord approx 1 approx 2 actual
x-coord approx 1 approx 2 actual
... ... ... ...
Is there a way for me to make this into a file which can be input easily into GUPlot? Many thanks.
Jack
This is already a format well suited for gnuplot. Look up help plot using from the gnuplot prompt.
In order to get this into a file, you can either pipe the standard output from your program (e.g. in Unix-like systems with yourprog > file.dat), or use the C function fprintf (with fopen and fclose).
This tutorial should help you http://www.duke.edu/~hpgavin/gnuplot.html

Resources