Andrew Clark in MATLAB Answers
上次活动时间: 2020-2-12

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);

关于 ThingSpeak

The community for students, researchers, and engineers looking to use MATLAB, Simulink, and ThingSpeak for Internet of Things applications. You can find the latest ThingSpeak news, tutorials to jump-start your next IoT project, and a forum to engage in a discussion on your latest cloud-based project. You can see answers to problems other users have solved and share how you solved a problem.