Receiving multiple math errors while plotting data with Matlab AQI Calculator Example
1 次查看(过去 30 天)
显示 更早的评论
I am receiving runtime errors when running this Matlab example [ MathWorks Internet of Things Team (2020). Air Quality Measurements and Visualizations (https://www.mathworks.com/matlabcentral/fileexchange/71908-air-quality-measurements-and-visualizations), MATLAB Central File Exchange. Retrieved February 7, 2020.] using my Thingspeak Public channel data. It sometimes runs successfully, but mostly ends with these error codes. There is obviously some sort of problem with the data being read. I have checked the data in the Channel and in the specific field and it looks ok. I have tried reducing the number of minutes ("NumMinutes") to a lower number but get same result. Perhaps there is a date or time issue, since I am asking for 1440 minutes, which is 24 hours. Can anyone shed any light on what could be causing these issues? Thanks much.
This is the code:
% This script acquires data from a public ThingSpeak channel located at
% the MathWorks Headquarters in Natick, MA, and uses it to calculate
% an Air Quality Index (AQI).
% Copyright 2019 The MathWorks, Inc.
% Prior to running this MATLAB code template, assign the channel ID to read
% data from to the 'readChannelID' variable. Also, assign the field ID
% within the channel that you want to read data from to plot.
% TODO - Replace the [] with channel ID to read data from:
%%readChannelID = 357142;
readChannelID = 891066;
yourChannel = 839634;
yourChannelWriteKey = 'xxxxxxxxxxxxxxxxxxx';
% TODO - Replace the [] with the Field ID to read data from:
fieldID1 = 2;
% Channel Read API Key
% If your channel is private, then enter the read API
% Key between the '' below:
readAPIKey = '';
%% Read Data %%
[rawData, time] = thingSpeakRead(readChannelID, 'Field', fieldID1, 'Numminutes', 1440, 'ReadKey', readAPIKey);
localTime = time - hours(7); % adjust for local time in Sunnyvale CA
%% Run custom function that analyzes collected data, computes AQI and plots collected data
returnAQI = analyzeplotAQI(localTime,rawData);
%% Send computed AQI to ThingSpeak Channel (Field 1)
thingSpeakWrite(yourChannel,returnAQI,'WriteKey',yourChannelWriteKey,'Fields',8);
% CUSTOM FUNCTIONS BELOW
% Main function that smoothes collected data and calls other custom functions
function returnAQI = analyzeplotAQI(localTime,rawData)
%% Smooth data
smoothData = movmedian(rawData,10);
% Find max and plot data
smoothDataMax = max(smoothData);
plotfun(localTime,rawData,smoothData,smoothDataMax)
% Combine smoothed data with time as # of elements are the same
smoothParticulateDataTable = table(localTime,smoothData,'VariableNames',{'Time','Particulate_Conc'});
% Calculate AQI
pmObs = round(mean(smoothParticulateDataTable{:,2}),1); % Calculate 24 hour running average
returnAQI = calculateAQI(pmObs);
end
%% Plot Data
function plotfun(localTime,rawData,smoothData,smoothDataMax)
plot(localTime, rawData);
hold on
plot(localTime,smoothData,'-*')
% Plot max of smooth data
line(localTime,smoothDataMax * ones(length(localTime),1),'LineStyle','--')
title('2.5 micron particulate concentration \mug/m^{3}')
xlabel('Time');
ylabel('Concentration \mug/m^{3}');
legend('Collected data','Smoothed data','Max of Smooth Data','Location','best')
axis tight
hold off
end
%% Calculate AQI
function returnAQI = calculateAQI(pmObs)
% Learn about how AQI is calcuated
% https://www.epa.gov/outdoor-air-quality-data
aqiLow = [0;51;101;151;201;301];
aqiHigh = [50;100;150;200;300;500];
concLow = [0;15.5;40.5;65.5;150.5;250.5];
concHigh = [15.4;40.4;65.4;150.4;250.4;500.4];
% Create Look Up Table
lutAQI = table(aqiLow,aqiHigh,concLow,concHigh,...
'VariableNames',{'AQI_low','AQI_high','PM_low','PM_high'});
% Find the necessary equation parameters
rowIdx = find(pmObs >= lutAQI.PM_low & pmObs <= lutAQI.PM_high);
PM_min = lutAQI.PM_low(rowIdx);
PM_max = lutAQI.PM_high(rowIdx);
AQI_min = lutAQI.AQI_low(rowIdx);
AQI_max = lutAQI.AQI_high(rowIdx);
returnAQI = round((((pmObs - PM_min) * (AQI_max - AQI_min))/(PM_max - PM_min)) + AQI_min);
end
And these are the Output error messages I am getting:
Warning: Ignoring extra legend entries.
> In legend>process_inputs (line 594)
In legend>make_legend (line 335)
In legend (line 279)
In AQI Calculator>plotfun (line 50)
In AQI Calculator>analyzeplotAQI (line 33)
In AQI Calculator (line 23)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise
multiplication, use '.*'.
Error in AQI Calculator>calculateAQI (line 71)
returnAQI = round((((pmObs - PM_min) * (AQI_max - AQI_min))/(PM_max - PM_min)) + AQI_min);
Error in AQI Calculator>analyzeplotAQI (line 38)
returnAQI = calculateAQI(pmObs);
Error in AQI Calculator (line 23)
returnAQI = analyzeplotAQI(localTime,rawData);
2 个评论
Walter Roberson
2020-2-7
For debugging of the first part, I suggest changing to
function plotfun(localTime,rawData,smoothData,smoothDataMax)
ax = gca;
h1 = plot(ax, localTime, rawData);
hold(ax, 'on')
h2 = plot(ax, localTime,smoothData,'-*');
% Plot max of smooth data
h3 = line(localTime,smoothDataMax * ones(length(localTime),1),'LineStyle','--');
title(ax, '2.5 micron particulate concentration \mug/m^{3}')
xlabel(ax, 'Time');
ylabel(ax, 'Concentration \mug/m^{3}');
legend([h1; h2; h3], {'Collected data','Smoothed data','Max of Smooth Data'},'Location','best');
axis(ax, 'tight')
hold(ax, 'off')
if ~isscalar(h1); fprintf('plotfun: first plot had %d lines??\n', length(h1)); end
if ~isscalar(h2); fprintf('plotfun: second plot had %d lines??\n', length(h2)); end
if ~isscalar(h3); fprintf('plotfun: third plot had %d lines??\n', length(h3)); end
Walter Roberson
2020-2-7
For the second part: check
size(pmObs - PM_min)
size(AQI_max - AQI_min)
and in particular I would recommend that you validate that you got some data from the read request.
回答(1 个)
Vinod
2020-2-11
My suspicion is your data contains 'NaN' and you are attempting to perform arithmetic on NaNs. You may want to look into functions like FILLMISSING to operate on your data before performing arithmetic on the matrices.
1 个评论
Walter Roberson
2020-2-12
Maybe, but I do not see how that would lead to array mismatch dimension errors in the * operator?
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Visualize Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!