LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Graph of a data file

Solved!
Go to solution

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

Nghtcwrlr

---------------------------------------------------------------------------------------------
*************************************
---------------------------------------------------------------------------------------------
0 Kudos
Message 1 of 7
(4,072 Views)

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?

 

 

Nghtcwrlr

---------------------------------------------------------------------------------------------
*************************************
---------------------------------------------------------------------------------------------
0 Kudos
Message 2 of 7
(4,068 Views)
Solution
Accepted by topic author Nghtcrwlr
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???

22156i53C269023EB69EB6

Nghtcwrlr

---------------------------------------------------------------------------------------------
*************************************
---------------------------------------------------------------------------------------------
0 Kudos
Message 3 of 7
(4,053 Views)

Just remove this line "memset(wave, 0, sizeof(wave));"

You don't need it.

0 Kudos
Message 4 of 7
(4,005 Views)

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"]

Nghtcwrlr

---------------------------------------------------------------------------------------------
*************************************
---------------------------------------------------------------------------------------------
0 Kudos
Message 5 of 7
(3,985 Views)

Permit me to object on your solution. Smiley Wink

 

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.

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 7
(3,980 Views)

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

 

// ---------------------------------------------------------------------------------------------------------

0 Kudos
Message 7 of 7
(3,942 Views)