Using Face color patch to display the temperature alert

warda Nemouchi on 9 Jun 2021
Latest activity Reply by Christopher Stapels on 10 Jun 2021

Hello, I want to create a sort of an alert to display the temperature health like in the image below:

i have found this code:

% This script uses the Air Quality Index (AQI) calculated 
% in "particulateConcentration" to display the appropriate text associated 
% with the index. 
% Copyright 2019 The MathWorks, Inc.
readChannelID = yourChannelID;
% TODO - Replace the [] with the Field ID to read data from:
fieldID1 = 1;
% Channel Read API Key 
% If your channel is private, then enter the read API
% Key between the '' below: 
readAPIKey = yourChannelReadKey;
returnAQI = thingSpeakRead(readChannelID, 'Field', fieldID1,'ReadKey', readAPIKey);
[airCondition, conditionIdx] = returnairhealth(returnAQI);
plotairhealth(airCondition, conditionIdx)
%% Plot Air Health
function plotairhealth(airCondition, conditionIdx)
vertices = [0 0; 1 0; 1 1; 0 1];
faces = [1 2 3 4];
% Use Look Up Table and air health condition to get color for reading
faceColorPatchLUT = [0 0.83 0; 0.44 0.95 0.52; 1 0.98 0.53; 0.95 0.98 0.22; 1 0.39 0.39; 1 0 0];
faceColorPatch = faceColorPatchLUT(conditionIdx,:);
airHealthPatch = patch('Faces',faces,'Vertices',vertices,'FaceColor',faceColorPatch);
hold on 
text(0.5,0.5,airCondition,'FontSize',16,'HorizontalAlignment','center')
hold off
% Remove Axis handle
axHandle = airHealthPatch.Parent;
axHandle.YTickLabel = [];
axHandle.XTickLabel = [];
end
%% Air Health Condition
function [airCondition,conditionIdx] = returnairhealth(returnAQI)
airCondition = {'Good';'Moderate';'Unhealthy for Sensitive Groups';'Unhealthy';'Very Unhealthy';'Hazardous'};
aqiLow = [0;51;101;151;201;301];
aqiHigh = [50;100;150;200;300;500];
lutAQI = table(airCondition,aqiLow,aqiHigh,'VariableNames',{'Air_Health','AQI_low','AQI_high'});
conditionIdx = find(returnAQI >= lutAQI.AQI_low & returnAQI <= lutAQI.AQI_high);
airCondition = string(lutAQI.Air_Health(conditionIdx));
end

However, i don't know howto adapt it to my case where i have only 2 cases : if my variable = 0 : the health is good and the color of the box is green else if my viariable = 1 ==> healt is dangerous and the color is red.

Christopher Stapels
Christopher Stapels on 10 Jun 2021

Start by writing what you want to do as pseudo code, just a list of steps like a recipe. Once you have the logic flow set out, then you can start to fill in the patch code.