Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ with embedded Gnuplot command

579 views
Skip to first unread message

Yong Huang

unread,
Feb 17, 1998, 3:00:00 AM2/17/98
to

I wanted to reply individually to
Hongwei Wang <hw...@ewl.uky.edu> but got the msg back from
their mail deamon. I think this is of general interest.

Hongwei Wang <hw...@ewl.uky.edu> says
------------------------------------------------
I am trying to write a CGI script with c++ and embedded gnuplot
command to draw a real time curve. Could you tell me how to embed this
gnuplot command in C++? Or maybe you can send me a very simple code with

embedded gnuplot.
-----------------------------------------------

Hi, Hongwei,

My CGI script calling gnuplot is very simple. If you write it in Perl
(I don't know C++), the following should work

#!/usr/local/bin/perl -w
require "cgi-lib.pl"; #use cgi.pm if you want the new version
&ReadParse; #but then you need change this and something
else
$userfunc=$in{'usrfuncfromHTML'};
open GPLOT, " | gnuplot" or die "Can't open gnuplot: $!\n";
print GPLOT, "plot $usrfunc";
#or something like print GPLOT, "set output 'myoutfile'";
#then print GPLOT, "plot $usrfunc";
close GPLOT;


In fact I wrote mine in csh because my script made quite a few calls to
launch other applications in the file system. To pass commands to
gnuplot in csh (or any unix shell script), I use here documents like
this:

gnuplot <<END
set output "myoutfile"
plot $1
END

supposing $1 is a command line argument containing a user inputted
function.

So, basically I can see two ways to pass commands to gnuplot, open a
pipe as in Perl and use here documents. There may be other ways.

Good luck.

Yong


hongwei Wang

unread,
Feb 17, 1998, 3:00:00 AM2/17/98
to

Thank you , Yong. I got your message in my email. But I don't know why you
got msg back. I have got some idea on how to write C++ calling gnuplot.
I will write a gnuplot script and then use system call in C++ to call this
script file. Now I meet a new problem. When you use gnuplot, usually you get
a postscript output graph(?), how can you convert it to gif file? Maybe there
are some tools can do it. I tried "pstogif" in my machine, but it doesn't work.
It is possible that some syntax error occurred.

Hongwei

In article <34E9B101...@columbia.shell.com>

Yong Huang

unread,
Feb 17, 1998, 3:00:00 AM2/17/98
to

If you're using gnuplot 3.6 and installed the GD library, you can do a
set term gif. If you're not (you're using 3.5 as I am),
set term pbm
set output "myfile.ppm"
plot "mydata.dat"
quit

At the unix prompt, do a ppmtogif -o "mygif.gif" myfile.ppm (I can't
remember the syntax for ppmtogif). If you don't have ppmtogif, do a Net
search on Netpbm at Yahoo or AltaVista.

If you have more questions, let me know.

Yong

hongwei Wang

unread,
Feb 18, 1998, 3:00:00 AM2/18/98
to

In article <1998021718...@proteus.shell.com>

yo...@shellus.com (Yong Huang) writes:

>If you're using gnuplot 3.6 and installed the GD library, you can do a
>set term gif. If you're not (you're using 3.5 as I am),
>set term pbm
>set output "myfile.ppm"
>plot "mydata.dat"
>quit
>
Hi, there. Thanks for your help and I got both curves. I was wondering why you
"set term pbm" and you can get file with ppm format. I found ppmtogif in
my machine, but the syntax is not correct. Even I use "man" command, I couldn't
get much help. I know we have pstogif tool, but it doesn't work well.

Hongwei
>in the unix prompt, do a ppmtogif -o "mygif.gif" myfile.ppm (I can't

walker

unread,
Feb 18, 1998, 3:00:00 AM2/18/98
to hongwei Wang

hongwei Wang wrote:
>
>....... I have got some idea on how to write C++ calling gnuplot.

> I will write a gnuplot script and then use system call in C++ to call this
> script file. Now I meet a new problem. When you use gnuplot, usually you get
> a postscript output graph(?), how can you convert it to gif file? Maybe there
> are some tools can do it. I tried "pstogif" in my machine, but it doesn't work.
> It is possible that some syntax error occurred.
>

Make progress already? Besides what I said in another news group with
you, here is a place you can find something useful, including "how do I
call gnuplot from my own programms" in an Unix example:

http://www.uni-karlsruhe.de/~ig25/gnuplot-faq/
or
ftp://rtfm.mit.edu/pub/usenet/news.answers/graphics/gnuplot-faq

Seems you are still using v3.5 rather than v3.6beta. Compiled binary
v3.6beta are available for different OS and work fine from my
experience. To get gif option, need gd1.2+v3.6 and compile them by
yourself. The above mentioned FAQ tells all.

Sorry, don't try mail, please

Yong Huang

unread,
Feb 18, 1998, 3:00:00 AM2/18/98
to

This is what I use in my script to convert ppm to gif

(ppmtogif ./tmp/$tmpvar.ppm1 | giftrans -t '#FFFFFF' -o ./tmp/$tmpvar.
gif1) >& /dev/null

After I completely switch to gnuplot 3.6beta, this needs to be
re-written.

Yong

Hongwei says-----------------------------

David Ishee

unread,
Feb 19, 1998, 3:00:00 AM2/19/98
to

HWA...@ukcc.uky.edu (hongwei Wang) writes:

> Thank you , Yong. I got your message in my email. But I don't know why you

> got msg back. I have got some idea on how to write C++ calling
> gnuplot.

The easiest way I know to talk to gnuplot with C++ is to use a named
pipe (works in UNIX, not sure about it in win95 etc). Actually you are
using C commands but they work in C++.

Example which works with gcc:

#include <stdio>

FILE* pipeVar;
pipeVar = popen("gnuplot","w"); /* opens writeable one-way pipe to
gnuplot. If not in path, errors
will occur. Return val of popen
will be NULL if error occurs.
see popen man page for details */

fprintf(pipeVar, "plot sin(x)\n"); /* sends command to gnuplot
Note: fprintf must be used,
cout won't work */

/* You can also send a variable containing the command instead of the
quoted string I show above, but it must be a char* string */

fflush(pipeVar); /* pipes are buffered, so flush buffer after you are
finished */

pclose(pipeVar); // close pipe when you are finished with your program

Hope that helps,

David

+--------------------------------------------------------------------+
| David Ishee |
| MS grad student, Mechanical Engineering dm...@ra.msstate.edu |
| Mississippi State University |
| |
+------------- http://www2.msstate.edu/~dmi1/index.html -------------+

hongwei Wang

unread,
Feb 20, 1998, 3:00:00 AM2/20/98
to

Now I use system call in C++. You can write a script called "work.gnu"
then you do this in C++:
system("gnuplot work.gnu");
I think it's much easier to do.

Hongwei

In article <m31zwyd...@gsubc.dot.edu>

walker

unread,
Feb 20, 1998, 3:00:00 AM2/20/98
to

hongwei Wang wrote:
>
> Now I use system call in C++. You can write a script called "work.gnu"
> then you do this in C++:
> system("gnuplot work.gnu");
> I think it's much easier to do.
>

Congratulation! You've made it work. Wish your gif option works well.

David Ishee

unread,
Feb 23, 1998, 3:00:00 AM2/23/98
to

HWA...@ukcc.uky.edu (hongwei Wang) writes:

> Now I use system call in C++. You can write a script called "work.gnu"
> then you do this in C++:
> system("gnuplot work.gnu");
> I think it's much easier to do.

Yeah, sure tell him the EASY way! What's this world coming to?

--

walker

unread,
Feb 24, 1998, 3:00:00 AM2/24/98
to

David Ishee wrote:
>
> HWA...@ukcc.uky.edu (hongwei Wang) writes:
> > Now I use system call in C++. You can write a script called
> > "work.gnu" then you do this in C++:
> > system("gnuplot work.gnu");
> > I think it's much easier to do.
>
> Yeah, sure tell him the EASY way! What's this world coming to?
>

Do you mean it is too easy to believe? No surprise to me. :-)

0 new messages