2

I need to pass a variable defined in my main program (plain C) to Gnuplot through a pipe. How can I do so? Any assistance is greatly appreciated.

Thanks

PS: Ubuntu 12.04.3 LTS, GCC 4.6.3

The code:

FILE *pipe = popen("gnuplot -persist","w");
    //fprintf(pipe, "bin_width = binsize; ");
    fprintf(pipe, "load 'Q3.gnu' \n");
    close(pipe);

EDIT: I have solved my own problem, sorry to take your time. The solution is:

FILE *pipe = popen("gnuplot -persist","w");
    fprintf(pipe, "binsize = %f;", (float)binsize);
    fprintf(pipe, "load 'Q3.gnu' \n");
    close(pipe); 
    return 0;
8
  • Show your code. How does your C program communicate with gnuplot? What do you mean by passing a variable? Are you using popen ? How?? Commented Dec 22, 2013 at 22:58
  • @BasileStarynkevitch I am writing the part of the code relevant to my argument. Commented Dec 22, 2013 at 23:00
  • @BasileStarynkevitch The variable binsize is defined in my source C code and I want to pass it to Gnuplot and use it for plotting purposes. I commented out that part since it ran into problems, also I would opt for directly making it usable by Gnuplot without renaming it if possible. Commented Dec 22, 2013 at 23:03
  • If you mean shell pipe | then just print to stdout what you want to feed to gnuplot. Otherwise, clarify the question. Commented Dec 22, 2013 at 23:04
  • @hyde Please have a look at my edited post, I am using piping inside my C source file as I indicated. If the user changes the binsize defined at the start of the code this should be passes to Gnuplot. Commented Dec 22, 2013 at 23:05

1 Answer 1

2

I can't give you an exact response, but gnuplot_i interface uses a pipe mechanism to comunicate with a gnuplot session. It seems very close to your request. I believe you can find useful information in the home page of the project (http://ndevilla.free.fr/gnuplot/).

Sign up to request clarification or add additional context in comments.

1 Comment

Well thanks for your interest, I will have a look at the link you mentioned.

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.