Matlab calcs no longer work for averages?

Ray Berkelmans on 30 Jan 2023
Latest activity Edit by Christopher Stapels on 31 Jan 2023

Hi
I used to have the following daily max, min and mean code working. But, this week it seems to work for max and mins but not means. Any idea what's going on?
writeAPIKey = 'xxxxxx';
% Define date range of 1 day
end1 = datetime('now')
start=datetime('today')
% Read Weight data from Field1 for last 24 hrs
[Weight, time] = thingSpeakRead(xxxxxx,'DateRange', [start,end1], 'Fields', [1]);
Wtmax = max(Weight); % Get 24hr Max
Wtmin = min(Weight); % Get 24hr Min
Wtav = mean(Weight); % Get 24hr Av
% display results
display(Wtmax, 'Wt_max'), display(Wtmin, 'Wt_min'), display(Wtav, 'Wtav');
Output:
end1 =
datetime
30-Jan-2023 14:00:35
start =
datetime
30-Jan-2023
Wt_max =
-0.0685
Wt_min =
-0.5190
Wtav =
NaN
Christopher Stapels
Christopher Stapels on 30 Jan 2023
I would suspect there are missing or nan values in the dataset. MATLAB is very explicit about how it handles these cases, but it may be different for mean than for max and min. If you find this is the case, have a look at the reference page for mean, and see the part about 'nanflag'.
Ray Berkelmans
Ray Berkelmans on 30 Jan 2023
OMG, that was indeed it! I didn't think to check because I could tell from my graphs (all my channels) that there were no missing values or NANs. What I didn't count on was that the very process of calculating a new value and inserting that into the db creates a new timestamp which shows up as an empty cell in all fields except for the newly calculated field. That is certainly a trap for newbies! Using 'omitnan' fixed the issue. I'm kinda wondering why this wouldn't be the default implementation of the Mean (or similar) calculation? Chris, thank you!
Christopher Stapels
Christopher Stapels on 31 Jan 2023 (Edited on 31 Jan 2023)
Generally it is best to put derived data into a new channel, not the same one. Thanks for letting me know it worked, you made my night! ( I like to help users)
Its probably not the default becasue if you had a regular data set with no nan, you might want to know if nan started showing up so you could make the decision to omit on purpose insted of on accident. Im not sure, though, I think that design review was before I joined MathWorks!
We actually have a lot of meetings where we try to guess how users would encouter issues in our functions and then design the function to best handle it if possible. Thanks for posting on the forum, if you get the chance, please share more detail about your project if you can.

Tags

No tags entered yet.