Variable AddValue vs Variable[0]


andrewjfox

Recommended Posts

Hi,

I'm having some trouble with the various data types. Although variables, channels and vchannels work similarly (in theory) I'm finding certain subtle differences and can't get the right type for what I want.

application:

I'm storing arrays as they come in over a network to a variable for later processing.

eg

Data.AddValue({{128,1,3,5,7}})

Data.AddValue({{129,2,4,6,8}})

Data = {{129,2,4,6,8},{128,1,3,5,7}}

then in processing loop the code looks for data with bit 7 set (data dirty bit) in first element, and clears this bit when data has been processed.


if(TestBit(Data[0][0],7))
Data[0][0] = ClearBit(Data[0][0],7)
// do some processing on Data
endif
[/CODE]

The processing loop also uses the timestamp of each array, Data.Time[0], as part of the processing

Problem:

- If Data is V type, then AddValue sets the time of all previously added arrays in history to the same value, so I can't use the timestamps to process the data

- If Data is channel, then trying to change an existing value (Data[0][0]) is the same as AddValue, so adds new entry with incorrect data, instead of changing the existing data at [0][0]

- If data is variable then, again, trying to change a single element Data[0][0] stuffs up the entire history

I'd like to be able to add arrays to a channel/variable, and be able to go through the history and change individual elements. Is this possible using the functions AddValue etc, or do I need to manually manipulate a standard array? I only want a history of about 100 arrays

Regards

Andrew

Link to comment
Share on other sites

You can't change data in a channel once its there. That basically goes for channels and V channels, so if you want to make edits, you are going to have to use a variable. Changing an element of a variable should not mess up the time stamps, but remember, the timestamps correspond to rows, the first dimension of the array only. Perhaps you can create a simple document to show the issue you are having with variables?

Link to comment
Share on other sites

I've written a small sequence to test variable history and see what is happening. I must admit it's not what I expected. (BTW I'm using latest version 5.87a)


private MyArray
MyArray.AddValue(InsertTime({{128,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}}, SysTime(), 0))
? MyArray
Delay(1)
MyArray.AddValue(InsertTime({{129,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}}, SysTime(), 0))
? MyArray
Delay(1)
MyArray.AddValue(InsertTime({{130,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}}, SysTime(), 0))
? MyArray
Delay(1)
MyArray.AddValue(InsertTime({{131,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}}, SysTime(), 0))
? MyArray
for(private i = 0, i<4, i++)
? Format("[%d], %s, %d ",i,FormatTime(MyArray.Time[i]),MyArray[i][0])
endfor
// all good till here
MyArray[0][0] = ClearBit(MyArray[0][0],7)
? MyArray
? MyArray.Time
// clearing the bit not only clears the whole array + history, but messes the timestamp as well
[/CODE]

Everything seems ok until I try writing to one of the array elements, then all of the data (inc history) gets wiped and the time stamps get messed up.

Would greatly appreciate you pointing out my problem, if it's a code issue, or alternatively advise a better solution.

Regards

Andrew

Link to comment
Share on other sites

There appears to be some issue with addValue() on variables. Fortunately there is an easy workaround:

myarray[0] = insertTime({{128,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}},systime(),0)

? myarray

delay(1)

myarray[1] = myarray[0,numrows(myarray)]

myarray[0] = insertTime({{129,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}},systime(),0)

? myarray

delay(1)

myarray[1] = myarray[0,numrows(myarray)]

myarray[0] = insertTime({{130,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}},systime(),0)

? myarray

delay(1)

myarray[1] = myarray[0,numrows(myarray)]

myarray[0] = insertTime({{131,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}},systime(),0)

Link to comment
Share on other sites

Thanks,

That seems to work well.

Just to let you know, I've also had problems with ClearBit function messing up timestamped data

// My array is timestamped as previous post

MyArray[0][0] = ClearBit(MyArray[0][0],7) // doesn't work as expected

// make sure return data is timestamped fixes problem (long winded but works)

MyArray[0][0] = InsertTime(ClearBit(MyArray[0][0],7), MyArray.Time[0], 0)

Thanks

Andrew

Link to comment
Share on other sites

Archived

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