2

Hey guys I am currently working with gnuplot.
I have this .csv file which I have been using to plot some formulas
(eg plot "filename.csv" u 0:day($0) = $0 ). The plots worked out; however, I was wondering if there was a way within gnuplot to save the output of my formulas as a data file too.

1 Answer 1

6

Please check the manual or in the gnuplot console type help table.

Code:

### save data as text
reset session

f(x) = x
g(x) = x**2
h(x) = x**3

set xrange[-5:5]
set samples 11

plot f(x) w lp, g(x) w lp, h(x) w lp

set table "myOutput.dat"
    plot '+' u 1:(f($1)):(g($1)):(h($1)) w table
unset table

### end of code

Edit:

Actually, to be more flexible with data separators (e.g. comma or whatever) in the output file, you could change the plot ... w table command to something like the line below. However, I guess, gnuplot will always add a leading space " " and a trailing TAB \t for each line. But maybe this can also be changed.

plot '+' u (sprintf("%g,%g,%g,%g",$1,f($1),g($1),h($1))) w table

Result:

enter image description here

And myOutput.dat:

 -5  -5  25  -125
 -4  -4  16  -64
 -3  -3  9   -27
 -2  -2  4   -8
 -1  -1  1   -1
 0   0   0   0
 1   1   1   1
 2   2   4   8
 3   3   9   27
 4   4   16  64
 5   5   25  125

Addition: (creating data in a loop)

With set print you are probably the most flexible, no leading space and trailing TAB. Check the manual or in gnuplot console type help set print.

Code:

### save data as text, independent of range and samples
reset session

f(x) = x
g(x) = x**2
h(x) = x**3

set print "myOutput.dat"
    do for [i=-5:5] {   
        # loop index only takes integers, multiply i with some factor if necessary
        print sprintf("%g,%g,%g,%g",i,f(i),g(i),h(i))
    }
set print
### end of code
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to creating them without calling the plot command?
of course you can skip the line plot f(x) w lp, g(x) w lp, h(x) w lp, but you need the line plot '+' u 1:(f($1)):(g($1)):(h($1)) w table. Well, there is another way to print into a file during a for loop, this might even be more clear. I will add this option later. In the meantime you might want to check help set print.

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.