Discussion:
GnuPlot problem from C ++
(too old to reply)
Ululì Ululà
2021-04-22 14:45:42 UTC
Permalink
I state that I am a beginner with C and C ++ and my English is bad.

I have to draw an f (x) on the Cartesian plane using C ++ and Gnuplot.
I have:

Windows 7 (64 bit)
DEVC ++ Embarcadero compiler Version 6.3
Gnuplot Version 5.2 patchlevel 8

I call Gnuplot from a DevC ++ program by passing it the string "Frase"
that contains the actions that Gnuplot will have to perform. When I
start the program the graph of f(x) does not respect the information
provided to Gnuplot with the string "Frase": for example the command
"unset key" it is not respected as well as the "set grid" command.
Why?

Below is the listing of my test program and I thank in advance whoever
succeeds to help me.


This is my program:

#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main(void) {

char Frase[300]="plot [1:4] x*x+1;set grid;unset key;set title
'f(x)';set xlabel 'X (m)';set ylabel 'Y (m)';set border \n";


int Funzione;
FILE * iFile, * oFile;

Funzione=4;

while (Funzione>0)
{
system("cls");
printf("Inserisci Funzione (0=EXIT) : ");
scanf("%d", &Funzione);

if (Funzione>0)
{
oFile = fopen("comando_A.txt", "wt");
fprintf(oFile,Frase);
fprintf(oFile,"pause -1");
fclose(oFile);
system("gnuplot comando_A.txt");
}

}

return(0);
}
Hans-Bernhard Bröker
2021-04-22 21:13:11 UTC
Permalink
When I start the program the graph of f(x) does not respect the
information provided to Gnuplot with the string "Frase": for example
the command "unset key" it is not respected as well as the "set grid"
command. Why?
Because the pieces of your command string are in the wrong order. You
put the cart before the horse.

You should really begin by using gnuplot manually for a bit, before you
go ahead trying to drive it from another program.
Ululì Ululà
2021-04-23 09:50:46 UTC
Permalink
On Thu, 22 Apr 2021 23:13:11 +0200, Hans-Bernhard Bröker
Post by Hans-Bernhard Bröker
When I start the program the graph of f(x) does not respect the
information provided to Gnuplot with the string "Frase": for example
the command "unset key" it is not respected as well as the "set grid"
command. Why?
Because the pieces of your command string are in the wrong order. You
put the cart before the horse.
You should really begin by using gnuplot manually for a bit, before you
go ahead trying to drive it from another program.
Your phrase "You put the cart before the horse" was enlightening so I
tried to move the horse in front of the cart and the program worked
correctly!


For the rest you are absolutely right: I have to practice more using
Gnuplot manually.

Thanks for the valuable advice and hello from Italy where I live!
Juan Humberto Artero Jaimez
2021-05-10 15:19:56 UTC
Permalink
When I start the program the graph of f(x) does not respect the
information provided to Gnuplot with the string "Frase": for example
the command "unset key" it is not respected as well as the "set grid"
command. Why?
Because the pieces of your command string are in the wrong order. You
put the cart before the horse.
You should really begin by using gnuplot manually for a bit, before you
go ahead trying to drive it from another program.
Loading...