2

I want my program to automatically open GNUplot and plot the graph from the given file. Unfortunetaly it does not work properlly:

system("gnuplot> plot '/home/user/Desktop/program/data.txt'");

The programm runs gnuplot but I constantyly get an error from gnuplot:

ivalid command

Moreover I checked in the command line and when typing:

gnuplot> plot '/home/user/Desktop/program/data.txt'

I also get the same error, but when first typing gnuplot and then

plot '/home/user/Desktop/program/data.txt'

it works properly.

Hence the conclusion is that I should use the second method in my programm (but I don't know how to put a few commands using system() or to fix the first method. Thanks for your help!!!

1
  • 2
    If you are getting the same error which you call from the command line, then this has nothing at all to do with your c code. Paying attention to things like that will help you to debug faster and better in the future. Already you know to look into how you should be calling gnuplot, which means you should read the documentation. Commented Nov 19, 2012 at 0:23

3 Answers 3

3

You have to invoke gnuplot like this:

gnuplot -p -e "plot '/home/user/Desktop/program/data.txt'"

And in C:

system("gnuplot -p -e \"plot '/home/user/Desktop/program/data.txt'\"");
Sign up to request clarification or add additional context in comments.

1 Comment

what do option -p and -e do? Please explain.
2

Try putting the gnuplot command plot '/home/user/Desktop/program/data.txt' into a file e.g. myfile, and telling gnuplot to read from it:

FILE* f = fopen("myfile", "wt");
fprintf(f, "plot '%s'\n", "/home/user/Desktop/program/data.txt");
fclose(f);
system("gnuplot < myfile");

1 Comment

this is my preferred approach, as you develop more complicated plot commands you'll have that file to review for debugging. Also saves some headache escaping things past the shell. In many cases its useful to save that file anyway, though obviously that depends on what you are doing.
2

Need to know the OS, but assuming a linux variant: > is output redirection. As you have already found out, it's not a valid command.

Best way to input is:

A) Check is gnuplot has a command line argument so it can take commands from the command line. e.g. gnuplot -command plot data.txt (that's a sample, I made it up. No idea if there is anything like '-command')

B) @anatolyg writes in his answer: you could write the commands to a file and then tell gnuplot to read that file. This is similar to method A. (don't forget to remove the temporary file after use)

C) Echo the commands and pipe to gnuplot. e.g. echo plot '/home/user/Desktop/program/data.txt' | gnuplot"

Comments

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.