08-23-2010 05:39 AM
I am having a data file with these values
1
1
1
-1
-1
-1
1
1
1
-1
-1
-1
I need to plot an XY graph with Y values as these data and
1,2,3,4,5,6,7,8,9,10,11,12 = X values.
How is this possible with a Graph control, to read the values inside the data file and plot th egraph??
thnx
Solved! Go to Solution.
08-23-2010 06:05 AM
I found out a command like this . but i how is it possible to obtain data from a text file already saved.
int PlotXY (int panelHandle, int control, void *xArray, void *yArray, size_t numberOfPoints, int xDataType, int yDataType, VAL_THIN_LINE, VAL_SIMPLE_DOT, VAL_SOLID, 1, VAL_RED)
How are the red colored parameters set?
08-23-2010 07:09 AM
static char file_name[MAX_PATHNAME_LEN]; static double wave[12]; void CVICALLBACK PlotGraph (int menuBar, int menuItem, void *callbackData,int panel) if (FileSelectPopup ("", "*.txt", "*.txt", "Name of File to Read", VAL_OK_BUTTON, 0, 1, 1, 0, file_name) > 0) { memset(wave, 0, sizeof(wave)); FileToArray (file_name, wave, VAL_DOUBLE, 12, 1, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_COLUMNS, VAL_ASCII ); DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, 1); PlotY (panelHandle, PANEL_GRAPH, wave, 12, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1,VAL_RED); } }
i tried to read the values inside the .txt file to an array using FileToArray function. Then i use this array to be plotted.
But I am getting a plot of just a straight line,. see figure wave. Any ideas why???
08-24-2010 04:49 PM
Just remove this line "memset(wave, 0, sizeof(wave));"
You don't need it.
08-25-2010 03:50 AM
ok...but what is the problem if we copy it to the memory.. is it similr to defining or allocating memery space??
Now i found out the problem. I need to give a much higher value for the array wave. Otherwise there comes an error telling insufficient array argument.....
static double wave["higher value than the size of array to be read"]
08-25-2010 04:58 AM
Permit me to object on your solution.
FileToArray scans exactly the number of elements you have asked it, so if you allocate an array of 12 elements and read 12 elements from the file you should get not errors, event if the file contains more elements.
The error you are receiving must depend on other reasons: in your actual code are you using variables for array dimension and / or number of elements in FileToArray? If so, are you sure they have the same value?
Additionally, in your PlotY you have set to identify points with a square, which I do not see in the graph: are you sure the graph you posted is the one traced by the code?
This kind of problems can be easily debugged by placing appropriate breakpoints and checking variables values. In your case I would place a breakpoint immediately after FileToArray and check the value of wave array (click on it and press Shift+F7. Additionally, is necessary to check the return value from FileToArray function, which can evidence possible errors found in the function.
08-26-2010 12:10 PM
If you already know number of data points you need to read, just declare your array with sufficient elements (higher is OK). If you don't, obtain the number of data points in your data file first then use dynamic memory allocation method to assign the values into your array.
// ---------------------------------------------------------------------------------------------------------
double *wave;
unsigned number_of_points;
(... obtain value of "number_of_points" first .....)
wave = malloc ( sizeof(double)*number_of_points ) ;
PlotY (panelHandle, PANEL_GRAPH, wave, number_of_points, VAL_DOUBLE,
VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1,VAL_RED);
.....
free (wave); // be sure to free the memory before exiting your program
// ---------------------------------------------------------------------------------------------------------