Why I am getting sum =NaN. Help, here is my code.

% Enter your MATLAB Code below
% Enter your MATLAB Code below
% Read Output Water Quantity over the past month from a ThingSpeak channel and write
% the average to another ThingSpeak channel.
% Channel 1035265 contains data from the MathWorks water supply station, located
% in Natick, Massachusetts. The data is collected once every day. Field
% 3 contains output liquid quantity data.
% Channel ID to read data from
readChannelID = 1035265;
% Output Liquid Quantity Field ID
outputliquidqantityFieldID = 3;
% Channel Read API Key
% If your channel is private, then enter the read API Key between the '' below:
readAPIKey = '';
% Get Output Liquid Quantity data for the last 30 days from the MathWorks water supply
% station channel. Learn more about the THINGSPEAKREAD function by going to
% the Documentation tab on the right side pane of this page.
meter_reading = thingSpeakRead(readChannelID,'Fields', outputliquidqantityFieldID,'NumDays',10,'ReadKey',readAPIKey);
sum = sum(meter_reading)
% Calculate the Cost
Billing_cost = 5*(meter_reading/1000);
display(Billing_cost,'Total Billing Cost (INR)');
% Start by setting the channel ID and Alert key. All alert key start with TAK.
formatSpec = "The Water consumption bill is: %d,%d";
A1 = 5*(meter_reading/1000);
A2 = meter_reading
apiKey = 'TAK14ZOZGAXZQMR05';
alertURL = "https://api.thingspeak.com/alerts/send";
options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", apiKey ]);
alertBody = sprintf(formatSpec,A1,A2)
alertSubject = sprintf(" Water consumption exceeded 100 l!");
if meter_reading >= 100
webwrite(alertURL, "body", alertBody, "subject", alertSubject, options);
end

 Accepted Answer

KSSV
KSSV on 29 May 2020
Moved: Voss on 3 Jan 2024
If data has any NaN value, sum will be NaN....you need to omit nan while summing. You can use nansum. Read about it.

3 Comments

Aman Kumar
Aman Kumar on 29 May 2020
Moved: Voss on 3 Jan 2024
i tried nansum but i am getting this error
nansum requires Financial sum toolbox which you don't have. You can try sum with nanflag....
sum(meter_reading,'omitnan')
Aman Kumar
Aman Kumar on 29 May 2020
Moved: Voss on 3 Jan 2024
Thank you so much, it worked. :)

Sign in to comment.

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2020a

Tags

Asked:

on 29 May 2020

Moved:

on 3 Jan 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!