I have a Fortran 90 program that outputs some data to a .txt file.
The data is to be plotted with gnuplot.
I was able to launch gnuplot with
CALL SYSTEM("start wgnuplot")
which is equivalent to type
start gnuplot
in the Windows command line prompt.
But then, I would like to have the program telling gnuplot what to do next, i.e., changing directory to the right one, and plotting my file.txt.
All in all this boils down to a simpler question:
How do I pass a command line in Windows that launches gnuplot and gives it some additional commands?
I tried to do that with something even easier like plotting y=x.
In a normal gnuplot windows this is just plot x.
From the cmd.exe (which is what is called by Fortran's CALL SYSTEM() )I've tried:
start wgnuplot plot x
start wgnuplot plot x -pause
start wgnuplot plot x -persist
start wgnuplot plot x -noend
start wgnuplot plot x /noend
And others, including every possible variant with or without quotation marks, for instance
start wgnuplot "plot x -persist"
etc.
So far the only one that works is the basic
start gnuplot
Which starts gnuplot indeed. But then I don't know how to add the next commands. Once I have a working command line input I believe I will just have to plop it into the CALL SYSTEM argument to have my Fortran program doing all the work.
I could only find instructions on how to achieve this on a UNIX-like machine, but not on Windows.
Any help would be appreciated.
Background info: Windows 8, Code::Blocks, gnuplot 5.0 patchlevel 1
you need to use named pipes which are very easy in C and unix:
http://tldp.org/LDP/lpg/node11.html
and see this answer:
https://stackoverflow.com/a/14027358/2743307
in Fortran and UNIX you can use the shell mkfifo command:
https://genomeek.wordpress.com/2012/05/07/tips-reading-compressed-file-with-fortran-and-named-pipe/
Related
I'm trying to pass some data to octave by writing to the windows command line from python in one single line, but I am getting an error.
I found some sample code at https://octave.org/doc/v4.4.0/Printing-and-Saving-Plots.html and I made a one line variation of it to suit my needs:
f=figure('Visible','off');plot([1,2,3,4]);pause(1);print(f,"myplot.pdf","-dpdflatexstandalone");
when I run this one line in octave itself, it works flawlessly, and I can find a file named myplot-inc.pdf in the octave folder (it will be in a different location in the future once i actually get the code to work). However, when I run it from command line using
octave --silent --persist --eval f=figure('Visible','off');plot([1,2,3,4]);pause(1);print(f,"myplot.pdf","-dpdflatexstandalone");
it gives this error:
error: 'test_plot' undefined near line 1 column 60
I would like to eventually run it without the --persist option, possibly even using -W to prevent octave from opening, if at all possible. For now, how can I make this code work from the command line in the same way it works in octave?
I figured out why this happened fairly quickly. since I'm running from command line, I have to use single quotes around 'myplot.pdf' and '-dpdflatexstandalone' instead of double quotes.
I need to make a Fortran program(.exe) that analyses data and builds a plot.
I've decided to do this by calling gnuplot from my Fortran program along with a gnuplot settings file.
When I do this from Windows Command Line:
wgnuplot -persist input.txt
it works fine. But when I call it from Fortran program like this:
h=SYSTEM("wgnuplot -persist input.txt")
that doesn't open gnuplot.
At the same point, if I simly write:
h=SYSTEM("wgnuplot")
it successfully opens gnuplot (but, obviously doesn't draw a plot)
How can I call a gnuplot with a gnuplot settings file from the Fortran program?
I've tried to specify the whole path to the files, that doesn't help.
Also I need "-persist" key to be there, because running a command without it in CMD:
wgnuplot input.txt
does not work for me.
I've also tried all that with EXECUTE_COMMAND_LINE command, and also tried to do this with C++ code like this:
int main(){
char command[100];
strcpy( command, "wgnuplot -persist 'gp.txt'" );
// strcpy( command, "wgnuplot" ); // but this one works alright!
system(command);
return(0);
}
But I still get the same result - I can't run gnuplot with an argument file.
What am I doing wrong? Is there another way to achieve my goal?
Well, it turned out, that the problem was with the input.txt file itself.
So actually this command works perfectly on Windows:
h=SYSTEM("wgnuplot -persist input.txt")
#shellter, #agentp, #chw21 Thank you for your help!
In Windows you can use the following command in Matlab to start a new instance of MATLAB which will run in the background (i.e. you can keep executing commands in your first version of MATLAB).
system('matlab &')
An analogous call in OSX,
system([matlabroot '/bin/matlab &'])
however results in the display of the splash image, then nothing. If I take out the ampersand, the new instance opens as expected. Unfortunately, this won't work for me, I really need to be able to control the first instance of MATLAB while the second is running.
Does anyone know why this discrepancy between the operating systems exists? By the way, I'm using OSX 10.7, Windows 7 64 bit, and MATLAB R2012a on Mac and R2012b on PC.
As some background, I'm trying to write a generic tester for an interactive command line interface that uses the input() function extensively.
Edit: I should have mentioned that the command
/Applications/MATLAB_R2012a.app/bin/matlab &
works as expected from the OSX terminal. In other words, a new instance of MATLAB opens and new commands can be entered into the terminal. So this problem seems to be specific to the system() function in OSX matlab.
Also, I tried adding that command to a bash script and calling the script from matlab, but had the same problem that I did with putting the command into the system() function.
Thanks
This is a long shot, but it might be happening because when you invoke the new instance of Matlab from Matlab with the system() command on Unix or OS X, the matlab_helper process forks and runs a shell process to run the new application. If you omit the ampersand, the shell blocks and waits for the program to finish, and system() waits for it, so the first Matlab locks up. And (here's the speculation part) if you add the ampersand, Matlab launches in the background, and then the forked shell exits, which then causes the new Matlab process to exit because its parent process (the shell) has exited. (Windows doesn't have the same parent/child process relationships, process launch mechanism, or shells, which would explain the different behavior.)
You could try prefixing the command with nohup, which protects processes from getting killed by SIGHUP, which might be what's happening here to your second Matlab process.
system(['nohup ' matlabroot '/bin/matlab &'])
You could also try using the OS X open command to launch a new independent instance. Something like this. You may need to fiddle with the options and path, but -n should be what gives you a new instance. It should be pointing at /Applications/MATLAB_R2012a.app; I'm assuming that's what matlabroot is returning on OS X.
system(['open -na ' matlabroot])
You could also try running it from the Java process-launching features from within Matlab instead of with system(). Runtime.exec() doesn't block like system() does, and there may be other quirks to system(), like the matlab_helper architecture. Try launching it with java.lang.Runtime from Matlab.
jrt = java.lang.Runtime.getRuntime();
newMatlabProcess = jrt.exec([matlabroot '/bin/matlab']);
You can try the other command line variants above using this mechanism too, and you may need to redirect stdout to /dev/null, since the new processes input and output are buffered in to that newMatlabProcess object.
You can use applescript to do this. I do something like this:
! osascript -e "tell application \"Terminal\" to do script \"cd `pwd`;matlab -nojvm -nosplash -r 'why'\""
This example opens a new Matlab instance, in the current directory, and runs the command "why". You can remove the "-nojvm" if you need java in your background Matlab process
I'm trying to show an animation using gnuplot
I got the following script:
plot ”heat1d.txt ” using 1:2 every :::a::a
pause 0.01
a=a+1
if ( a<b ) reread
that I execute using
a = 0
b = 100
load "a.plot"
it works, but is there a way to execute all of this using only 1 command from a shell?
Alternatively is there a way to integrate the variable definitions into the .plot file so that I can simply execute it? I tried different things like echo 'a=0'|gnuplot etc but it doesn't seem to actually define the variable correctly
thanks
You can use a do for loop.
do for [a = 0:100] {
plot ”heat1d.txt ” using 1:2 every :::a::a
pause 0.01
}
The default terminal on linux is usually wxt, and it has the raise option, which will change the focus to the plot window at every iteration. This will make it difficult if not impossible to stop the animation.
I suggest to put noraise as the terminal option. For example, you can put the following line at the beginning of the script:
set term wxt noraise
Now, if you want to stop the animation halfway, press CtrlC on the gnuplot terminal.
You can pass -e as a commandline argument. For example, if you have the script:
#script test.gp
print foo,bar
Then you could run it with gnuplot using:
gnuplot -e "foo=1;bar=2" test.gp
In your case, it looks like you could accomplish nearly what you want by invoking your script as:
gnuplot -e "a=0;b=100" a.plot
Is it possible to determine - from within the script - whether the script is running in the R-GUI (specifically R.app on OS X) or whether it has been called from Terminal/command line (i.e. R --vanilla -f script.R)? If so, how is this possible?
I'm asking because I have a script that can run parallelized (using the doMC library), which should not be used from the GUI. Sometimes I need to further process the data calculated in the script, so I'd like to call the script from the GUI on these occasions.
Perhaps you are looking for interactive()?
In C, you would use the isatty function. If you could find an equivalent function in R (probably in a UNIX or file system library), that should help.