I Need Help With Isempty Function


Recommended Posts

I have a sequence that reports the temperature trend over time.  It reports a 1-minute trend, 5-minute trend, and 1-hour trend.  However, I am having problems with the portion that checks the data to make sure there we have enough readings to have recorded long enough.

 

For example, this is the first loop, that checks if I have at least 1 minute of data.

while ( isempty(thermister1[systime()-60]) ) // only continue if there is data one minute out
   delay(10)
endwhile

An earlier version of my code was successful, but it worked differently.  It stored the start time of my program in a variable, and simply checked it against the current time.  However, I was working to change it, thinking the isempty function would be a more direct test if the data were available.  

 

It seems that I am not properly understanding the isemtpy function.  I have not been able to find an area in the help file that refers to the usage of this function for for channel history.  If I give a index number for an array, would it return a 1 if that index had not been filled yet?

 

One other related question.  If I have my channel recording at 1-sec intervals, and my program started at a time interval at perhaps 1/2 second from a 1-sec clock tick, would my data all be recorded at 1/2-sec clock-ticks?  Would that cause there to be no data at the exact intervals of 1-sec?

 

Thank you for the help.

 

-Joe

Link to comment
Share on other sites

First, when subsetting by time, you really need to specify a range.  Doing thermister1[system()-60] will only return a value if there is a data point with a time stamp that is exactly (and I mean exactly to the microsecond) 60 seconds old.  That is very unlikely.  

 

In your case you want:

 

thermister1[systime(), systime()-60]

 

Second, if the array exists and you are checking if there is anything in the range, consider using numRows() instead.  IsEmpty() works, but its really designed to check for variables that never got anything assigned to them.

 

As to your last question, Channel Timing aligns it self to the second, so if you specify a timing of 1 and offset of 0, it will do its best to do it at x.000 even if the program started at x.500.

Link to comment
Share on other sites

I have the isempty statement working.  Though, to get it working I had to use your range method, which starts at 0 and goes back to the time I really want to check. E.g.

if (!isempty(thermister1[systime(),systime()-3601])

This would work.  But, if I put something like ...

if (!isempty(thermister1[systime()-3550,systime()-2601])

it would not work, and always reports it as being empty.  I was initially thinking that it would be better to check a smaller range, rather than a longer one.  for 1 minute it would not matter, but for an hour ago, I was concerned that the program would have to check the entire set of recorded data for an hour.

 

Perhaps it is the whole range reference method that I am having problems with. I am continuing to have a puzzling error referencing to the range of data. First, note that there is over an hour of data recorded.  So, there should be over 3600 seconds of data.

   if (!isempty(thermister1[systime(),systime()-3601])) // only do if there is data one HOUR ago
      x = mean(Thermister1[0,systime()-60]) // average of last minute
      y = mean(Thermister1[systime()-3540,systime()-3599])   // Avg Temperature 1 Hour ago for 1 min
      Trend_Temper_Hr = x-y  // Trend in units of degrees per HOUR.
   endif 

This code generates the error on the line that starts y=..

C1086 One of the parameters was empty: Temp_Trend_1_Minute Line 40 - Uncaught error in sequence Temp_Trend_1_Minute

 

Now, if I change the line to say, 

y = mean(Thermister1[systime(),systime()-3600])

Then it works fine.  But, of course, I only want an average from 59 minutes ago to 60 minutes ago. So, the above is not usable.

 

-Joe

 

p.s. When we use the "insert code" function on the forum, we are offered the option of putting a starting line number. But, it never works.

Link to comment
Share on other sites

The forum is a common 3rd party tool so we have no control of its bugs.  I didn't even know you could do line numbers!

 

Line 3 of your code (which I guess is actually line 39) is wrong.  You are mixing subsetting.  Remember, DAQFactory can subset by index or by time.  To determine which to use, it looks at the values inside the [].  If the smaller parameter is > 1e8 then its assumed to be subsetting in time.  You have 0, systime()-60.  0 is < 1e8, so it thinks you are subsetting by index.  systime()-60 is about 1.5 billion, so basically you are getting the entire array.  The last minute would be [systime(), systime()-60]

 

Line 4 (line 40) is syntactically correct.  However, I can't say if your data has data in that time range.  The best thing to do is use ? to debug:

 

? thermister1[systime()-3540, systime()-3599]

 

I think you will find that it will print nothing, meaning there is no data in that range.  Expand the range until you get data to see what you've done wrong.

 

Remember also that History of a channel is in samples, not seconds.  When subsetting by time, the units is seconds.  When subsetting by index on a channel, the units is samples.  So, if your thermistor1 channel has a history of 3600 but is being sampled faster than once per second, the history is going to run out before an hour's worth of data is reached.

 

Finally, DAQFactory uses a binary tree to find the start and end points, so don't worry about the amount of time required to subset by time.  Its certainly slower than subsetting by index, but the search is quite fast on any modern PC, especially if the data points are equally spaced.

Link to comment
Share on other sites

For some reason, my code numbers did show up.  I think it may just not show up in the preview, but do show up on the actual posting.  But, unfortunately there seems to be a variation on how the numbers show up.  You refer to a line 4, also being line 39.  But, I don't see any line numbers that high. Can we somehow be seeing different line numbers?

 

These little hidden rules about how a function works are quite frustrating. But, I gather that to consistently use time, I should always subtract from current system time 

You said that my line #3 mixes syntax, and then quote, "You have 0, systime()-60."  But, perhaps you are refering to what is showing as line #2, on my screen.  I show line #3 as;

  1. y = mean(Thermister1[systime()-3540,systime()-3599]) // Avg Temperature 1 Hour ago for 1 min

Is that the correct syntax?

 

I will mention that I did a lot of ? to debug, but failed miserably.  But that is probably because I was unaware of the time vs. index mode switching.

 

-Joe

Link to comment
Share on other sites

I was extrapolating to get 39 based on the fact you said the y= line was line 40.  And I either am more tired than I think, or the system changed the line numbers after I posted.  Probably the 1st!  Subtract 1 from the line numbers I specified, so when I said line #3, I meant #2.

 

The y= line is correct.

 

The x = line is incorrect and mixes syntax.  It should be [systime(), systime()-60]

 

As for hidden rules, they aren't really hidden.  Its described in 4.6 of the users guide, though I will admit there could use some examples using systime() since that's a pretty common thing. But then the forum tends to pick up where the user's guide drops off.

Link to comment
Share on other sites

Archived

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