readChannelID = 123456; WeightFieldID = 1; readAPIKey = 'ABCDE'; AnzTage = 16; 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 Weight = [data(:,WeightFieldID)]; 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); %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): Gewicht(i) = mean(DayWeight(end-9:end)); %Let's go to next day... i = i + 1; end %Differentiate the weight to get the day-to-day differences barh(Zeit(2:AnzTage),diff(Gewicht),0.8) %Bar chart horizontal %bar(Zeit(2:AnzTage),diff(Gewicht),0.8) %Bar chart %Summarize the day-to-day differences to get the cumulated weight change CumWeight = sum(diff(Gewicht)); %Now let's draw the plot title('Tägliche Gewichtsveränderung'); xlabel(['Veränderung gesamt: ' num2str(CumWeight,2) ' kg']) %ylabel('kg') %uncomment for Bar chart grid on