Dear all,
on a Windows 7 64 bit system, gnuplot 5.0.0, official 32 bit version,
the attached code ends with "File doesn't factorize into full matrix".
The code generates data, writes it into a binary file, and attempts to
plot it by calling gnuplot.
On Ubuntu, gnuplot 4.6.4, it works fine. To see how the output should
look like, you can "toggle" between binary and ASCII data by
"wdata_bin(tmpdat, data, row, col);" versus "wdata_ascii(tmpdat, data,
row, col);" and changing the "#" tag in the splot call accordingly.
Is this a bug or am I doing something wrong?
Cheers
Clemens
#include "stdio.h"
#include "stdlib.h"
typedef unsigned int uint;
void wdata_ascii(const char *fname, const double *data, const uint row,
const uint col)
{
FILE *wfile = fopen(fname, "w");
uint x, y;
for(x = 0; x < row + 1; x++)
for(y = 0; y < col + 1; y++)
{
if(!y && !x)
fprintf(wfile, "%u", col);
else if(!x && y != 0)
fprintf(wfile, "%u", y - 1);
else if(!y && x != 0)
fprintf(wfile, "%u", x - 1);
else
fprintf(wfile, "%g", data[(x - 1) * col + y - 1]);
if(y == col)
fprintf(wfile, "\n");
else
fprintf(wfile, " ");
}
fclose(wfile);
}
void wdata_bin(const char *fname, const double *data, const uint row,
const uint col)
{
uint x, y;
float *tsp = (float *)malloc((row + 1) * (col + 1) * sizeof(float));
for(x = 0; x < row + 1; x++)
for(y = 0; y < col + 1; y++)
if(!y && !x)
tsp[0] = col;
else if(!x && y != 0)
tsp[x * (col + 1) + y] = y - 1;
else if(!y && x != 0)
tsp[x * (col + 1) + y] = x - 1;
else
tsp[x * (col + 1) + y] = (float)data[(x - 1) * col + y - 1];
FILE *outfile = fopen(fname, "w");
fwrite(tsp, sizeof(float), (row + 1) * (col + 1), outfile);
fclose(outfile);
free(tsp);
}
void plotdata(const double *data, const uint row, const uint col)
{
char *tmpdat = "pltdat",
*cmdtmp = "pltcmd";
fprintf(stdout,
"\nthis will create or overwrite the files '%s' and '%s'. press
enter to continue.\n",
tmpdat, cmdtmp);
getchar();
wdata_bin(tmpdat, data, row, col);
// wdata_ascii(tmpdat, data, row, col);
FILE *gnufile = fopen(cmdtmp, "w");
fprintf(gnufile,
"set term png size 1024, 768\n" \
"set yl 'y'\n" \
"set xl 'x'\n" \
"set out 'test.png'\n" \
"splot '%s' binary matrix using 2:1:3 w pm3d palette t ''\n"
"#splot '%s' nonuniform matrix u 2:1:3 w pm3d palette t ''\n",
tmpdat, tmpdat);
fclose(gnufile);
system("gnuplot pltcmd");
remove(tmpdat);
remove(cmdtmp);
}
int main(void)
{
uint x, y,
row = 100, col = 200;
double *dat = (double *)malloc(row * col * sizeof(double));
for(x = 0; x < row; x++)
for(y = 0; y < col; y++)
dat[x * col + y] = (double)(x << 1) * y / 100.;
plotdata(dat, row, col);
free(dat);
return 0;
}
|