Dawn
unread,Jan 4, 2018, 1:56:36 PM1/4/18You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
Hi, all:
I used fopen in c to read in a data file. I used plot 'data.txt' matrix with image to plot out the image. However, when I used fopen to write in the same data set as a new data file and I used same command line with new data file as: plot 'data1.txt' matrix with image, the image cannot be plotted and error message said:
matrix does not represent a grid
My basic codes are:
------------------------------------------------------
int main()
{
#ifdef WIN32
FILE *pipe = _popen(GNUPLOT_NAME, "w");
#else
FILE *pipe = popen(GNUPLOT_NAME, "w");
#endif
int xsize = 20, ysize = 41;
float **din;
// alloc memory
din = (float **)malloc(sizeof(float *) * ysize);
for (int i = 0; i < ysize; i++) {
din[i] = (float *)malloc(sizeof(float) * xsize);
}
// read in 2D data
FILE *fp, *fp1;
fp = fopen("wedgegas.txt", "r");
fp1 = fopen("inversedwedgegas.txt", "w");
// read in data
if (fp == NULL) {
printf("fp is NULL!\n");
exit(1);
}
for (int i = 0; i < ysize; i++) {
printf("i=%d \n ", i);
for (int j = 0; j < xsize; j++) {
fscanf(fp, "%f ", &din[i][j]);
printf("%f ", din[i][j]);
}
fscanf(fp, "\n");
printf("\n ");
}
/* write final inversed data to din */
if (fp1 == NULL) {
printf("fp1 is NULL!\n");
exit(1);
}
for (int i = 0; i < ysize; i++) {
printf("%d \n", i);
for (int j = 0; j < xsize; j++) {
fprintf(fp1, "%f ", din[i][j]);
printf("%f ", din[i][j]);
}
fprintf(fp1, "\n");
printf("\n");
}
/* ---------------------------- plot results ------------------------------------------------------- */
plot:
if (pipe != NULL)
{
// plot sinc wave plot 1
fprintf(pipe, "set multiplot layout 1,2 \n"); // set multiple plots
fprintf(pipe, "plot 'wedgegas.txt' matrix with image \n"); // plot type
fprintf(pipe, "%s \n", "e"); // termination character, end a plot
fflush(pipe); // flush the pipe
fprintf(pipe, "plot 'inversedwedgegas.txt' matrix with image \n"); // plot type
fprintf(pipe, "%s\n", "e"); // termination character
fflush(pipe); // flush the pipe
// wait for key press
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.get();
#ifdef WIN32
_pclose(pipe);
#else
pclose(pipe);
#endif
}
else
std::cout << "Could not open pipe" << std::endl;
fclose(fp);
fclose(fp1);
return 0;
}
------------------------------------------------------------
Any suggestions? Thanks in advance!