Running gnuplot from Fortran program under Windows - windows

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!

Related

How can I print an octave plot to a .pdf file from the windows command line?

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.

Passing command line arguments to gnuplot in Windows

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/

write octave/matlab output to clipboard

iam looking for a way to get the output of an octave statement to the windows clipboard.
iam not searching a way to just manually copy/paste text from the cmd window (i know how this would work). iam also not looking for getting the whole output of a complete octave session which could be gotten by launching octave with a script to execute and piping all output to some clip.exe.
i want to capture the output from some single statement that will be executed from octave promt or some function or script.
Would be great if someone has some advice.
Edit:
from a comment i learned about the clipboard command of matlab that is unfortunally not implemented yet in octave.
maybe any other ideas involving fancy system() calls?
Well, apparently it's not too difficult to implement something fairly similar to Matlab - after a few minutes of fiddling around, behold my new clipboard.m:
function clipboard(data)
if ~ischar(data)
data = mat2str(data);
end
data = regexprep(data, '\\','\\\\');
data = regexprep(data, '%','%%');
f = tempname;
h = fopen(f, 'w');
fprintf(h, data);
fclose(h);
system(['clip.exe < ' f]);
delete(f);
end
You could always call something like xclip through a system command. For examples of xclip usage, see here
The following Matlab command works for putting multiline stuff into the clipboard on Mac. Presumably you would substitute pbcopy with xclip and it would work on linux.
>> system(['echo "line1' 10 'line2' 10 'line3" | pbcopy'])

Run a function using Octave from terminal

I want to run a function - let's say test - inside my test.m file. I want to run this function using Octave from terminal. So, it should be something like:
$>/Users/me/octave/bin/octave test(param1,param2)?
How can I accomplish this? I can do that in Matlab. But I didn't find a way in Octave.
You can use octave --eval CODE
Octave's docs on this can be found here. To run a function with input args:
In Terminal cd to your working directory.
Type octave to open an interactive session.
ls to check that your function's file is in your working dir, cd to the dir if not.
Type the function's name immediately followed by the input args in brackets in the correct order, Eg: >foo(100). Your function will then run, spitting out whatever you print as well as all the results for lines of code not ending with ;.

Problem with input filter using doxygen 1.6.3 on windows XP

I am trying to use doxygen to generate documentation for some matlab classes I have written. I am using the doxygen-matlab package, which includes a perl script to kludge matlab .m files into c++ style commented files, so that doxygen can read them.
In my doxyfile, I have set (according to the instructions)
FILTER_PATTERNS = *m=C:/doxygenMatlab/m2cpp.pl
However, when the code runs, rather than running the script on the input files, it appears to just open the script using whatever the default windows setting for .pl is.
IE, if I associate .pl with notepad, the script is opened by notepad once for each input file doxygen is trying to parse. If I associate .pl with perl.exe, the script runs and throws the no argument error
Argument must contain filename -1 at C:\doxygenMatlab\m2cpp.pl line 4.
The doxygen documentation says
Doxygen will invoke the filter program by executing (via popen()) the command <filter> <input-file>
So I am wondering if there is some problem with popen() and windows that I could fix.
Could you try the workarounds I posted on the Matlab File Exchange regarding the doxygen package ?
Set the following variables in the Doxyfile :
INPUT_FILTER=perl m2cpp.pl
FILE_PATTERNS=*.m
If it doesn't work you should try to install ActivePerl : with this version of perl, everything is working fine.
I tried to reproduce the error using the Windows command prompt ("cmd") and noticed the following:
If you call "perl m2cpp.pl" you get error -1 because you did not specify a m-file to be translated into a cpp-file.
If you call "perl m2cpp.pl mfile" and the path of mfile contains spaces, you get error 1.
After I moved the mfile into a location which does not contain spaces, I got the desired output.
Now back to Doxygen. I tried what you suggested, Fabrice, without any luck. I read the doxygen help and found out that the INPUT_FILTER variable is only read and used if FILTER_PATTERNS is empty.
Therefore, I now use INPUT_FILTER = "C:\Programme\MATLAB\R2009a\sys\perl\win32\bin\perl U:\doxygen_matlab\m2cpp.pl" and an empty FILTER_PATTERNS variable. With this configuration, you can even leave the PERL_PATH variable empty. Moreover, there seems to be no issues with file names that contain spaces.
Unfortunately, all files are parsed with the above configuration, not only m-files. However, setting FILTER_PATTERNS to something like *.m=C:\Programme\MATLAB\R2009a\sys\perl\win32\bin\perl U:\doxygen_matlab\m2cpp.pl does not work because doxygen automatically adds the name of the filtered mfile and interprets the command as perl "m2cpp.pl mfile". Of course, the file "m2cpp.pl mfile" does not exist, because these are two files.
Maybe you can find a solution to this problem. In the meantime, I suggest the workaround above and that you keep your C-files away from the folder that contains the m-files.
write a simple batch file, e.g. mfilter.bat, which takes one argument from command line:
C:\Programme\MATLAB\R2009a\sys\perl\win32\bin\perl U:\doxygen_matlab\m2cpp.pl %1
Change setting in Doxyfile:
FILTER_PATTERNS = *.m=mfile.bat
This did it for me (on a Windows platform)
I think I solved this problem : it came from a bad association between .pl and the program to execute (maybe due to a bad installation of the perl shipped whith Matlab ?).
To correct this, you should change the association for the .pl files : in a Windows command prompt ("cmd"), just type de 2 following lines :
assoc .pl=PerlScript
ftype PerlScript=C:\Program Files\MATLAB\R20xx\sys\perl\win32\bin\perl.exe %1 %*
(the old installation forgot the %* at the end, the arguments were not passed to the m2cpp.pl script).
And then everything should be fine with the FILTER_PATTERNS set the usual way, for example FILTER_PATTERN=*m=C:\DoxygenMatlbab\m2cpp.pl
Could you tell me if this fixed your problem ?
According to the Doxygen forums, there is a difference in behavior between using INPUT_FILTER and FILTER_PATTERNS.
I found that if I do some extra (escaped) quoting, I can get FILTER_PATTERNS to work. For example, instead of:
FILTER_PATTERNS = "*.m=sed -e 's|%%|//!|'"
Try:
FILTER_PATTERNS = "*.m=\"sed -e 's|%%|//!|'\""
(All of my experimentation was done with doxygen version 1.8.6)

Resources