Sie sind auf Seite 1von 9

gnuplot_i 2.

Page 1 sur 9

gnuplot_i.c
Go to the documentation of this file.
00001
00002
00003 /*-------------------------------------------------------------------------*/
00017 /*--------------------------------------------------------------------------*/
00018
00019 /*
00020
$Id: gnuplot_i.c,v 2.10 2003/01/27 08:58:04 ndevilla Exp $
00021
$Author: ndevilla $
00022
$Date: 2003/01/27 08:58:04 $
00023
$Revision: 2.10 $
00024 */
00025
00026 /*--------------------------------------------------------------------------00027
Includes
00028 ---------------------------------------------------------------------------*/
00029
00030 #include "gnuplot_i.h"
00031
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <stdarg.h>
00036 #include <assert.h>
00037
00038 #ifdef WIN32
00039 #include <io.h>
00040 #endif // #ifdef WIN32
00041
00042 /*--------------------------------------------------------------------------00043
Defines
00044 ---------------------------------------------------------------------------*/
00045
00046 /*--------------------------------------------------------------------------00047
Prototype Functions
00048 ---------------------------------------------------------------------------*/
00049
00059 char const * gnuplot_tmpfile(gnuplot_ctrl * handle);
00060
00070 void gnuplot_plot_atmpfile(gnuplot_ctrl * handle, char const* tmp_filename,
char const* title);
00071
00072 /*--------------------------------------------------------------------------00073
Function codes
00074 ---------------------------------------------------------------------------*/
00075
00076 /*-------------------------------------------------------------------------*/
00087 /*--------------------------------------------------------------------------*/
00088
00089 gnuplot_ctrl * gnuplot_init(void)
00090 {
00091
gnuplot_ctrl * handle ;
00092
int i;
00093
00094 #ifndef WIN32
00095
if (getenv("DISPLAY") == NULL) {
00096
fprintf(stderr, "cannot find DISPLAY variable: is it set?\n") ;
00097
}
00098 #endif // #ifndef WIN32
00099
00100

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

gnuplot_i 2.x

Page 2 sur 9

00101
/*
00102
* Structure initialization:
00103
*/
00104
handle = (gnuplot_ctrl*)malloc(sizeof(gnuplot_ctrl)) ;
00105
handle->nplots = 0 ;
00106
gnuplot_setstyle(handle, "points") ;
00107
handle->ntmp = 0 ;
00108
00109
handle->gnucmd = popen("gnuplot", "w") ;
00110
if (handle->gnucmd == NULL) {
00111
fprintf(stderr, "error starting gnuplot, is gnuplot or gnuplot.exe in
your path?\n") ;
00112
free(handle) ;
00113
return NULL ;
00114
}
00115
00116
for (i=0;i<GP_MAX_TMP_FILES; i++)
00117
{
00118
handle->tmp_filename_tbl[i] = NULL;
00119
}
return handle;
00120
00121 }
00122
00123
00124 /*-------------------------------------------------------------------------*/
00135 /*--------------------------------------------------------------------------*/
00136
00137 void gnuplot_close(gnuplot_ctrl * handle)
00138 {
00139
int
i ;
00140
00141
if (pclose(handle->gnucmd) == -1) {
00142
fprintf(stderr, "problem closing communication to gnuplot\n") ;
00143
return ;
00144
}
00145
if (handle->ntmp) {
for (i=0 ; i<handle->ntmp ; i++) {
00146
00147
remove(handle->tmp_filename_tbl[i]) ;
00148
free(handle->tmp_filename_tbl[i]);
00149
handle->tmp_filename_tbl[i] = NULL;
00150
00151
}
00152
}
00153
free(handle) ;
00154
return ;
00155 }
00156
00157
00158 /*-------------------------------------------------------------------------*/
00181 /*--------------------------------------------------------------------------*/
00182
00183 void gnuplot_cmd(gnuplot_ctrl * handle, char const * cmd, ...)
00184 {
00185
va_list ap ;
00186
00187
va_start(ap, cmd);
00188
vfprintf(handle->gnucmd, cmd, ap);
00189
va_end(ap);
00190
00191
fputs("\n", handle->gnucmd) ;
00192
fflush(handle->gnucmd) ;
00193
return ;
00194 }
00195
00196
00197 /*-------------------------------------------------------------------------*/

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

gnuplot_i 2.x

00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00248
00249
00250
00251
00252
00253
00254
00255
00256
00265
00266
00267
00268
00269
00270
00271
00272
00273
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00329
00330
00331
00332
00333
00334

Page 3 sur 9

/*--------------------------------------------------------------------------*/
void gnuplot_setstyle(gnuplot_ctrl * h, char * plot_style)
{
if (strcmp(plot_style, "lines") &&
strcmp(plot_style, "points") &&
strcmp(plot_style, "linespoints") &&
strcmp(plot_style, "impulses") &&
strcmp(plot_style, "dots") &&
strcmp(plot_style, "steps") &&
strcmp(plot_style, "errorbars") &&
strcmp(plot_style, "boxes") &&
strcmp(plot_style, "boxerrorbars")) {
fprintf(stderr, "warning: unknown requested style: using points\n") ;
strcpy(h->pstyle, "points") ;
} else {
strcpy(h->pstyle, plot_style) ;
}
return ;
}

/*-------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
void gnuplot_set_xlabel(gnuplot_ctrl * h, char * label)
{
gnuplot_cmd(h, "set xlabel \"%s\"", label) ;
}

/*-------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
void gnuplot_set_ylabel(gnuplot_ctrl * h, char * label)
{
gnuplot_cmd(h, "set ylabel \"%s\"", label) ;
}

/*-------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
void gnuplot_resetplot(gnuplot_ctrl * h)
{
int
i ;
if (h->ntmp) {
for (i=0 ; i<h->ntmp ; i++) {
remove(h->tmp_filename_tbl[i]) ;
free(h->tmp_filename_tbl[i]);
h->tmp_filename_tbl[i] = NULL;
}
}
h->ntmp = 0 ;
h->nplots = 0 ;
return ;
}
/*-------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
void gnuplot_plot_x(
gnuplot_ctrl
*
double
*
int

handle,
d,
n,

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

gnuplot_i 2.x

00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429

Page 4 sur 9

char

title

)
{
int
i ;
FILE*
tmpfd ;
char const * tmpfname;
if (handle==NULL || d==NULL || (n<1)) return ;
/* Open temporary file for output
*/
tmpfname = gnuplot_tmpfile(handle);
tmpfd = fopen(tmpfname, "w");
if (tmpfd == NULL) {
fprintf(stderr,"cannot create temporary file: exiting plot") ;
return ;
}
/* Write data to this file */
for (i=0 ; i<n ; i++) {
fprintf(tmpfd, "%.18e\n", d[i]);
}
fclose(tmpfd) ;
gnuplot_plot_atmpfile(handle,tmpfname,title);
return ;
}

/*-------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
void gnuplot_plot_xy(
gnuplot_ctrl
*
handle,
double
*
x,
double
*
y,
int
n,
char
*
title
)
{
int
i ;
FILE*
tmpfd ;
char const * tmpfname;
if (handle==NULL || x==NULL || y==NULL || (n<1)) return ;
/* Open temporary file for output
*/
tmpfname = gnuplot_tmpfile(handle);
tmpfd = fopen(tmpfname, "w");
if (tmpfd == NULL) {
fprintf(stderr,"cannot create temporary file: exiting plot") ;
return ;
}
/* Write data to this file */
for (i=0 ; i<n; i++) {
fprintf(tmpfd, "%.18e %.18e\n", x[i], y[i]) ;
}
fclose(tmpfd) ;
gnuplot_plot_atmpfile(handle,tmpfname,title);
return ;
}

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

gnuplot_i 2.x

00430
00431
00432
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513

Page 5 sur 9

/*-------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
void gnuplot_plot_once(
char
*
title,
char
*
style,
char
*
label_x,
char
*
label_y,
double *
x,
double *
y,
int
n
)
{
gnuplot_ctrl
*
handle ;
if (x==NULL || n<1) return ;
if ((handle = gnuplot_init()) == NULL) return ;
if (style!=NULL) {
gnuplot_setstyle(handle, style);
} else {
gnuplot_setstyle(handle, "lines");
}
if (label_x!=NULL) {
gnuplot_set_xlabel(handle, label_x);
} else {
gnuplot_set_xlabel(handle, "X");
}
if (label_y!=NULL) {
gnuplot_set_ylabel(handle, label_y);
} else {
gnuplot_set_ylabel(handle, "Y");
}
if (y==NULL) {
gnuplot_plot_x(handle, x, n, title);
} else {
gnuplot_plot_xy(handle, x, y, n, title);
}
printf("press ENTER to continue\n");
while (getchar()!='\n') {}
gnuplot_close(handle);
return ;
}
void gnuplot_plot_slope(
gnuplot_ctrl
*
handle,
double
a,
double
b,
char
*
title
)
{
char const *
cmd
= (handle->nplots > 0) ? "replot" : "plot";
title
= (title == NULL)
? "(none)" : title;
gnuplot_cmd(handle, "%s %.18e * x + %.18e title \"%s\" with %s",
cmd, a, b, title, handle->pstyle) ;
handle->nplots++ ;
return ;
}

void gnuplot_plot_equation(
gnuplot_ctrl
*
h,

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

gnuplot_i 2.x

00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579

Page 6 sur 9

char
char

*
*

char const *
title

cmd

equation,
title

)
{
= (h->nplots > 0) ? "replot" : "plot";
= (title == NULL)
? "(none)" : title;

gnuplot_cmd(h, "%s %s title \"%s\" with %s",


cmd, equation, title, h->pstyle) ;
h->nplots++ ;
return ;
}

int gnuplot_write_x_csv(
char const * fileName,
double const * d,
int n,
char const * title)
{
int
i;
FILE*
fileHandle;
if (fileName==NULL || d==NULL || (n<1))
{
return -1;
}
fileHandle = fopen(fileName, "w");
if (fileHandle == NULL)
{
return -1;
}
// Write Comment.
if (title != NULL)
{
fprintf(fileHandle, "# %s\n", title) ;
}
/* Write data to this file */
for (i=0 ; i<n; i++)
{
fprintf(fileHandle, "%d, %.18e\n", i, d[i]) ;
}
fclose(fileHandle) ;
return 0;
}
int gnuplot_write_xy_csv(
char const *
fileName,
double const
*
x,
double const
*
y,
int
n,
char const
*
title)
{
int
i ;
FILE*
fileHandle;
if (fileName==NULL || x==NULL || y==NULL || (n<1))
{
return -1;
}

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

gnuplot_i 2.x

00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603 }
00604
00605 int
00606
00607
00608
00609
00610
00611 {
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645

Page 7 sur 9

fileHandle = fopen(fileName, "w");


if (fileHandle == NULL)
{
return -1;
}
// Write Comment.
if (title != NULL)
{
fprintf(fileHandle, "# %s\n", title) ;
}
/* Write data to this file */
for (i=0 ; i<n; i++)
{
fprintf(fileHandle, "%.18e, %.18e\n", x[i], y[i]) ;
}
fclose(fileHandle) ;
return 0;

gnuplot_write_multi_csv(
char const *
fileName,
double const
** xListPtr,
int
n,
int
numColumns,
char const
*
title)
int
int
FILE*

i;
j;
fileHandle;

if (fileName==NULL || xListPtr==NULL || (n<1) || numColumns <1)


{
return -1;
}
for (j=0;j<numColumns;j++)
{
if (xListPtr[j] == NULL)
{
return -1;
}
}
fileHandle = fopen(fileName, "w");
if (fileHandle == NULL)
{
return -1;
}
// Write Comment.
if (title != NULL)
{
fprintf(fileHandle, "# %s\n", title) ;
}
/* Write data to this file */
for (i=0 ; i<n; i++)
{
fprintf(fileHandle, "%d, %.18e", i, xListPtr[0][i]) ;

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

gnuplot_i 2.x

Page 8 sur 9

00646
for (j=1;j<numColumns;j++)
00647
{
00648
fprintf(fileHandle, ", %.18e", xListPtr[j][i]) ;
00649
}
00650
fprintf(fileHandle, "\n");
00651
}
00652
00653
fclose(fileHandle) ;
00654
00655
return 0;
00656 }
00657
00658 char const * gnuplot_tmpfile(gnuplot_ctrl * handle)
00659 {
static char const * tmp_filename_template = "gnuplot_tmpdatafile_XXXXXX";
00660
00661
char *
tmp_filename = NULL;
00662
int
tmp_filelen = strlen(tmp_filename_template);
00663
00664 #ifndef WIN32
00665
int
unx_fd;
00666 #endif // #ifndef WIN32
00667
00668
assert(handle->tmp_filename_tbl[handle->ntmp] == NULL);
00669
00670
/* Open one more temporary file? */
00671
if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
00672
fprintf(stderr,
00673
"maximum # of temporary files reached (%d): cannot open more",
00674
GP_MAX_TMP_FILES) ;
00675
return NULL;
00676
}
00677
00678
tmp_filename = (char*) malloc(tmp_filelen+1);
if (tmp_filename == NULL)
00679
00680
{
00681
return NULL;
00682
}
00683
strcpy(tmp_filename, tmp_filename_template);
00684
00685 #ifdef WIN32
00686
if (_mktemp(tmp_filename) == NULL)
00687
{
00688
return NULL;
00689
}
00690 #else // #ifdef WIN32
00691
unx_fd = mkstemp(tmp_filename);
00692
if (unx_fd == -1)
00693
{
00694
return NULL;
00695
}
00696
close(unx_fd);
00697
00698 #endif // #ifdef WIN32
00699
00700
handle->tmp_filename_tbl[handle->ntmp] = tmp_filename;
00701
handle->ntmp ++;
00702
return tmp_filename;
00703 }
00704
00705 void gnuplot_plot_atmpfile(gnuplot_ctrl * handle, char const* tmp_filename,
char const* title)
00706 {
char const *
cmd
= (handle->nplots > 0) ? "replot" : "plot";
00707
00708
title
= (title == NULL)
? "(none)" : title;
00709
gnuplot_cmd(handle, "%s \"%s\" title \"%s\" with %s", cmd, tmp_filename,
00710
title, handle->pstyle) ;

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

gnuplot_i 2.x

Page 9 sur 9

00711
handle->nplots++ ;
00712
return ;
00713 }
00714
00715
00716 /* vim: set ts=4 et sw=4 tw=75 */
Last modified: Sun Apr 8 2012 17:28:19

http://ndevilla.free.fr/gnuplot/gnuplot_i/gnuplot__i_8c_source.html

14/06/2012

Das könnte Ihnen auch gefallen