Graphing two global arrays against each other


ariesgo

Recommended Posts

I have a sequence that writes some values to a couple of global arrays. These arrays always have the same size and I'd like to plot the N values of one against the N values of the other one. I have simplified the original problem to the steps I describe below.

First, in order to check whether I would be able to do this sort of thing, I created a 2D graph, enabled the "Lin" style for the "Bottom Axis" in the "Axes" tab, and set both the X and Y expressions to a hard-coded array of values in the "Traces" tab, as follows:

Y Expression: {0,1,2,3}

X Expression: {0,1,2,3}

As I expected, using the Point plotting method for the Y axis I get four data points at (0,0), (1,1), (2,2) and (3,3).

Next, I tried to replace one of the above expressions with a global array variable which is written to by a sequence. So, I added a GenerateSamplePoints sequence with the code:

global sampleXArray = NULL

function GenerateSamplePoints()
  if(IsEmpty(sampleXArray))
	sampleXArray[0] = 0
	sampleXArray[1] = 1
	sampleXArray[2] = 2
	sampleXArray[3] = 3
  endif
endfunction

Then I ran that code through a button that calls it as a quick sequence action, and replaced the X Expression with:

X Expression: sampleXArray

This works fine and I get the same data points (0,0), (1,1), (2,2), (3,3) as before. Similarly, if I assign sampleXArray to the Y Expression and assign the hard-coded array {0,1,2,3} to the X expression, things work fine too.

But I run into a problem when I add another global array sampleYArray to the GenerateSamplePoints sequence above and try to plot one against the other. Even if I simply set both X Expression and Y Expression to the same sampleXArray, I fail to get the expected points (0,0), (1,1), (2,2) and (3,3). Instead, all the points appear at the bottom as if I were plotting the {0,1,2,3} array against {0,0,0,0}.

So, what am I doing wrong here? Why can I plot a global array against a hard-coded array or a hard-coded array against a global array, but not two global arrays against each other?

I attach a .ctl file that shows this problem. There are four graphs on the initial Page_0. The top-left one, which uses the two global array variables, is the one that doesn't display the expected points, whereas the other ones, which use a hard-coded array ("{0,1,2,3}") in either the X Expression or the Y Expression or both, show the correct points.

graphtest.ctl

Link to comment
Share on other sites

The problem is alignment. Once you created the second variable, you have both axes with arrays that have time associated with them. The static array: {1,2,3,4} doesn't have time, but when you create the global variable, every time you do:

sampleXArray[3] = 3

not only does it put 3 in the 4th element, but it also assigns the time that the command was executed to the time portion of the array (which is just another array with the same number of rows).

The default alignment is 0.5, which means it will go along the x axis and find the first point that matches within 0.5 seconds of that point. Since all your data points are created within milliseconds of each other, that means its always going to match the first data point. This is why you get a graph that looks like its plotting against {0,0,0,0}

Anyhow, the solution is to simply turn alignment off by going to the bottom axis and setting the Align Threshold to -1.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.