If you specify a channel name in an expression, the entire channel history array will be returned. In some cases this may be exactly what you want, especially in graphs. In some cases, you will just want the most recent data point or some subset of your data. To get a subset of your data, put the desired range of points in brackets after the channel name. For example, ChannelName[0,4] will return the last five data points. You can get a single point as well: ChannelName[0] will return the most recent data point. Subsetting can actually occur at any point, for example: Sin(chan * otherchan[0])[0,49] will return the first 50 data points of the sin() of the entire history of chan multiplied by the most recent value of otherchan.
Note: A common error is to use () instead of [] for subsetting. Although this works in some cases, it does not work in all, nor do we guarantee that it will continue to work in newer versions.
Note: Whenever possible, put the subset notation immediately after the channel or variable name. This allows DAQFactory to optimize the data retrieval. Otherwise, DAQFactory is forced to copy the entire channel history or variable array before subsetting. So, while you could do Sin(chan * otherchan[0])[0,49] it is much better to do sin(chan[0,49] * otherchan[0]). There are of course cases where you can't do this, which is why DAQFactory supports subsetting pretty much anywhere. Subsetting immediately after the channel name is especially important when channel persistence is used.
Some advanced subsetting is possible beyond just a range of indices.
You can specify time ranges in your subsetting, and instead of using the data point number, DAQFactory will use the times of your data points to determine which points get returned.
chan[5h,6h] will return all the values between 5 and 6 o'clock today. Of course this only works if the array has time associated with it, so ({1,2,3})[5h,6h] won't work, though ({1,2,3})[0,1] will return {1,2}
You can subset variables by specifying an array in the subset and the result will be an array of values with the given index of the original variable. An example probably best explains this:
private x = {1,2,3,4}
private y = {1,3}
? x[y]
Result: {2,4}
Since y is an array and not a scalar, DAQFactory will return an array of two elements, the same as y, where each element is the element of x indicated by the element of y. You can repeat, so if:
y = {1,3,2,2,0}
? x[y]
Result: {2,4,3,3,1}
Note that this only currently works on variables where the subsetting is right after the variable name. So: x[y] will work, but (x)[y] will not, and will simply do the same as: (x)[y[0]].
You can put calculations in your subsets.
channel[Abs(otherchannel[0])]
There are many creative uses for this feature.
For two and three dimensional arrays you can subset on multiple dimensions by repeating the [] notation.
channel[3][2][1] returns the data point in the 4th row, 3rd column, 2nd depth.
By not specifying a dimension, the entire dimension is returned.
For example, given channel is 3 dimensional:
channel[3][2] returns a 1 dimensional array of data points corresponding to the 4th row, 3rd column.
channel[][2][1] returns a 1 dimensional array of data points corresponding to the 3rd column, 2nd depth.
You can also specify ranges:
channel[0,2][4][2] returns 3 values on the first three rows, 5th column, 3rd depth.
The first specifier is always rows. Rows always corresponds to time, and therefore you can only subset in time along rows. So, channel[0][5h][2] is not valid, but channel[5h][2][4] is.