Gnuplot won't open window when called via CPP file - gnuplot

I'm trying to get some practice with C and C++ by playing with gnuplot, but whilst following an example on plotting a simple 2 column data file, I encountered an issue.
When I call gnuplot in my script, it refuses to open any window, and doesn't appear to do any work at all. I don't think this is an issue of window persistence, as I don't even see it open for a split second. I've tried fiddling with setting a persistent terminal and pausing after plotting to no avail. My CPU's activity meter never spikes as it does when I run my other example functions, leading me to believe it never opens in the first place.
However, the strangest part to me is that I managed to plot the data file with gnuplot just how I'd hoped to, but the only way I was able to get it to work was running gnuplot as a terminal command via system().
Below is my function I'm testing with. As the comments note, that system call seems to work perfectly, but I'd prefer to use gnuplot "regularly," not via system calls.
void ex2() {
Gnuplot gp("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\"");
// system("gnuplot -p -e \"plot '2col.dat'\""); #works for some reason?????
gp << "plot '2col.dat'\n";
std::cin.get();
}
I'd appreciate any help you can offer! I just want to be able to plot the file with the "gp <<" notation. I apologize if this is similar to another thread here, but every similar one I've found was solved by adding "pause mouse close" at the end or changing the terminal persist setting, which have not worked for me.

Related

How to stop GNUPLOT 5.4.5 after defining DATA command and how to clear command window

I have big troubles when manual inputing data acc. to: https://stackoverflow.com/questions/69532716/simple-line-from-two-points-continuing-through-points
When i start to typing data, I mean when i define points and want to correct sth - I cannot do that.
GNU plot starts to using very strange chars. And i cannot clear any data I already defined.
I dont even know how to stop gnuplot and continue my job.
Can anyone help me how to correct my data or maybe how to re-run gnuplot with simple command?
Is it also possible to clear command window in GNUPLOT 5.4.5 (windows)?
I tried to use CTRL - C, CTRL - D commands to return to default typing and it does not work. For clearing command window i tried to use clear or CLC or system 'cls' command and cannot achieve required effect :/

is gnuplot fit influenced by terminal?

i have a gnuplot script file to fit measurement data, using this structure:
set terminal png
...some format templates...
f(x) = a + b*x + c*x**2
fit f(x) "datafile.txt" using "X":"Y" via a, b, c
...some plotting commands etc...
with this, gnuplot shows some strange behaviour:
when i run the script as-is, it gives me the following error:
Undefined value during function evaluation
"myscriptfile.gnuplot", line 5: error during fit
when i move the set terminal png line after the fit line, it runs without a hassle.
normally, i'm loading this at the beginning of a master script containing format templates and further data processing routines. doing this also gives me the aforementioned error message, even with the moved set terminal command.
since this is just the first part of processing my data i really need it to work from the master script... i already tried setting initial guesses, FIT_LIMIT and loading it from a gnuplot environment. i'm using gnuplot 4.6.5.
does anyone know how to solve this or how fit gets influenced by other commands? or is this some kind of bug?
edit: uploaded a stripped down version of scripts and data files to here. with the reduced data files the computed fits don't concur with the measured points, but with the complete data they do.
I'm not sure, what the real error is, but it seems to be related to your use of using "PHEAT":"RHOT", although that should be fine.
I could reproduce your error with the following minimal setup:
A data file test.dat:
A B
1 2
2 3
3 4
and a file test.gp:
f(x) = a*x**2 + b*x + c
fit f(x) 'test.dat' using "A":"B" via a,b,c
If I call this file with gnuplot test.gp I get the same error as you. It doesn't appear if I use using 1:2. If I paste the code in an interactive terminal, the error also appears, but only once. If I repeat only the fit command again, it works fine. I'll report this as a bug.
In the script you posted, I was also able to fix this by using using 9:8 instead of using "PHEAT":"RHOT". Additionally you must remove the first line of the data file, which can be done on-the-fly with tail, so that you can leave the using statements of the plot unchanged. So you can use:
fit rhotside(x) "< tail -n +2 testdata.txt" using 9:8 via rhot0, rhot1, rhot2
fit rcoldside(x) "< tail -n +2 testdata2.txt" using 9:8 via rcold0, rcold1, rcold2

Simple example for plotting x-y graphic in visual studio c++ 2008 (console win32)?

After reading almost all I found here and in google, I've not found a simple and direct solution/example about plotting x-y values in c++ (console app win32, the black one) in visual studio 2008. Based on this post, I should use MsChart controls, would anyone be willing please to share a simple example about this?
This post, and many others, talk about libraries that don't belong to visual studio (so they won't compile with the EXE, hence it won't be included as part of the resulting EXE) or having the graphic displayed with excel. I just want to get a simple x-y graphic (and line going through these points), not worried at all if it is the ugliest and simplest graphic in the whole world, but it must appear automatically after my code run (perhaps in a new window or perhaps inside the console?) AND if I run this EXE in another pc, thousands of kilometers away from my pc, the graphic will still appear after the code runs.
Am I asking something too complex? boring?? I hope I could get some answers/examples/solutions instead of this post being closed =) Thanks in advance!
#Koushik thanks again for your support! even though I'm getting the whole picture I need a clear example to get it right, so to voteup I'd like to test this simple example:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double f(const double &x) {
return (
sin(x)*exp(-x));
}
int main()
{
unsigned Nplot = 100;
double x_low = 0.0;
double x_high = 20.0;
double h = (x_high - x_low)/(double)Nplot;
for (unsigned i=0;i<=Nplot;i++)
{
double x = x_low + i*h;
cout << setw(20) << x <<
setw(20) << f(x) << endl;
}
system("pause>nul");
return 0;
}
this is a pretty basic code to generate a sin(x) function. If you copy/paste this in console application Win32 in VC++ 2008 (new project/Visual C++/Win32/console application Win32/empty project/), the data will appear in the screen:
Then, putting these values in Excel gives:
I want this, BUT the output window should appear after the values appear in the console, and more important, the code must not depend on excel or something else external to the visual C++. So, writing some additional code to draw this line and after compiling, both must appear/work in any computer, especially in those without VS or any libraries, just running the EXE in windows, that's all. So, is it possible? could you share a simple code example to draw this line (to add it to the above code)? thanks!
you can call gui application or rather any other app by using the system("appname");. include the stdlib.h header. what you can do is create your console app and prepare the data. store those data in a file, open the app which can read the data from the file and plot it.
The gui application will not be so difficult to write either, if your goal is to only plot the data.in console you really cant draw things. in turbo c++ we had this option because it was mainly created for dos.
Algorithm:
1) prepare the data say time along x axis and amplitude along y axis. store this in a file using your own format.eg: x-ais data y-axis data .
2).Prepare a gui app that can plot the graph. use CreateGraphics() methods of the form to draw lines i.e the axes and the data line. CreateGraphics::drawLine() and CreateGraphics::drawLines() should help your progress.
EDIT:
you can pass command line args to the GUI app. this argument could be the data file path
The simplest thing would be for your console application, after computing the data, to create a window on screen (yes, console applications are allowed to use GUI) and then handle WM_PAINT with BeginPaint/PolyLine/EndPaint, using some appropriate scaling that makes the data (1) rightside up and (2) fit to the window.
You won't have any grid lines or axis labels, just the plotted line, but that's the bare minimum and can easily be done without either using or recreating an entire graphing library.
For an example of how to add GUI to your program, try Raymond Chen's scratch program, C++ version. Quite a bit of code to create just one window, but lucky for you, the only thing you have to change is the PaintContent() function.
Using Polyline is almost as straightforward as you could ask for -- it accepts an array of datapoints.
If you really want to add gridlines and labels, more Polyline calls and the ExtTextOut function will do the trick, but soon after that you'll want CreatePen and SelectObject to give control over line colors and styles (dotted, dashed, etc). And pay attention to the order -- draw the bottom things first so that later calls can cover over the top.
On the other hand if a single executable is not a requirement, I suggest you ship a copy of Gnuplot (the pgnuplot executable is designed for invocation from another program). First use wgnuplot to tune your plot options, then pass them to pgnuplot along with your data. You'll get reasonable scaling, gridlines, and labels for free this way, as well as exporting the result into common formats such as PNG.

Matlab command window tabs (tabbed)

Hi I hope this question conforms to community guidelines. While working in Matlab I'd really appreciate if the command window had tabs (much like most terminal emulators in Linux). Is available in Matlab or do I have to run different instances of Matlab?
I am running a system which produces plots so running Matlab in text based mode via -nojvm is not possible.
EDIT: Is there a way to get such a feature or do I have to wait for Mathworks to wake up and implement this simple, timesaving tool.
Hmm I think the way Matlab works (with the workspace and the editor) tabs might not be that beneficial in terms of performance as opposed to just running a new instance of Matlab.
In Linux, you can fork new MATLAB instances with unix command and pass the MATLAB command you want to run as a command line argument. If you add & to the end of the unix input string, the new MATLAB instance becomes a background process, so this way from one MATLAB command window you can run commands (with output) in several MATLAB instances, opening a new MATLAB instance for command execution and output.
Here's the code:
multithread.m:
function multithread(MatlabCommand)
% this is a function to create a new MATLAB instance and run a command in it.
unix([ 'matlab -desktop -r ', MatlabCommand, ' &' ]);
return
testfunction.m:
function testfunction()
fprintf('one two three.\n');
return
Then you can run MATLAB commands in a new instance this way: multithread('testfunction');.
As far as I know, there is no such feature.
Although are you aware that you can use the -nodesktop flag to run Matlab in the current console and still have the ability to bring up plots?
Well - depends on what you plan to do with the tabs. You do have tabs for file editor, variable editor. But I suppose you mean command window.
I sometimes have two or more instances of Matlab open - one doing some calculation and doing some observation and quick stuff from the other - but I don't find it advisable.
I have found against using multiple instances that the preferences and path get "unusable" regularly (say once every month).
But as Matlab is "single" threaded - while it's calculating it's near impossible to do simple tasks as editing code or open a file in the same instance. If it had tabs i imagine it even worse. I think this problems lie very deep inside how Matlab works so that will not change very soon.

Print Plot in Octave in Background

Currently, I use print -dpng foo.png to print a plot to file in Octave 3.0.1 on Ubuntu.
Sometimes, I generate thousands of images in a loop.
Whenever a new image pops up, it grabs the mouse control precluding me from multitasking.
Is there anyway to print silently or quietly?
It would be easier to answer your question with a little more information about what you are doing. But with a little guessing maybe this is what you need:
f = figure
set(f, "visible", "off")
plot([1,2,3,4])
print("MyPNG.png", "-dpng")

Resources