readChannelID = 123456; WeightFieldID = 1; TempFieldID = 3; readAPIKey = 'ABCDE'; AnzTage = 200; StartDate = datetime('today') - days(AnzTage) %Alternatively Start at a specific date %StartDate = 'June 5, 2019'; %Read data from StartDate until yesterday ('today' liefert gestern(??)) [data,time] = thingSpeakRead(readChannelID,'DateRange',[StartDate,datetime('today')]); %Determine number of days in dataset DaysInRange = ceil(days((max(time) - min(time)))); %Extract Weight and Temperature Weight = [data(:,WeightFieldID)]; Temperature = [data(:,TempFieldID)]; i = 1; while i <= AnzTage & i <= DaysInRange %Find max value of each day %Let's do some datetime magic ;-) BeginDay = day(min(time) + days(i-1)); BeginMonth = month(min(time) + days(i-1)); BeginYear = year(min(time) + days(i-1)); EndDay = day(min(time) + days(i)); EndMonth = month(min(time) + days(i)); EndYear = year(min(time) + days(i)); BeginDate = datetime(BeginYear,BeginMonth,BeginDay); EndDate = datetime(EndYear,EndMonth,EndDay); index = isbetween(time,BeginDate,EndDate); %find all measurements of this day Zeit(i) = min(time(index)); %Now let's get the max weight value of each day (however this method is sensitive to outliers...) %Gewicht(i) = max(Weight(index)); %It may be more reliable to get the mean value of each day %Gewicht(i) = mean(Weight(index)); %Even more intelligent way to find the "real" weight: DayWeight = Weight(index); DayTemp = Temperature(index); %Take the last 10 values of each days - according to my experience %it seems the most stable time for getting the weight values %(this is later than 10 p.m. with a measurement interval of 15 minutes, may be adjusted). %Take the very last value only: Gewicht(i) = mean(DayWeight(end:end)); MaxTemp(i) = max(DayTemp(1:end)); MinTemp(i) = min(DayTemp(1:end)); %Let's go to next day... i = i + 1; end %Summarize the day-to-day differences to get the cumulated weight change CumWeight = sum(diff(Gewicht)); %Now let's draw the plot yyaxis right plot(Zeit,MaxTemp,Zeit,MinTemp,'LineWidth', 2) %Line chart ylabel('Außentemperatur [°C]'); yyaxis left plot(Zeit,Gewicht,'LineWidth', 2.5) %Line chart ylabel('Gewicht [kg]') %uncomment for Bar chart title('Min-/Max-Temperatur und Gewicht'); xlabel(['Veränderung gesamt: ' num2str(CumWeight,2) ' kg']) grid on