How to plot the mean value of each year in one point only ( EACH 1st july ) ?
6 views (last 30 days)
Show older comments
Hello ,
i have daily temperature data ( 2006 - 2100)
i want to plot the yearly temperature value only in one point which is each 1st july (of each year) how can code that please ?
Thank you !
..........................................
Here is my code
%% Temperature Model 85 Rcp
clear all
close all
clc
Temperature85 = xlsread("filename.xlsx","Rcp85", "B3:B34700" ); %import data
writematrix(Temperature85, 'Temp85.txt');
Temp85= readmatrix("Temp85.txt");
year0_85=2006; % srtarting year
temps85=timetable(Temp85,'RowTimes',datetime(year0_85,1,1:height(Temp85))); % time table for daily temperature data
yearlyavg85 = retime(temps85, 'yearly', 'mean'); % calculate the mean temperature for each year
t85=temps85;
%extract years
t85.Year = year(temps85.Time,"iso");
yearlyavg85.Year = year(yearlyavg85.Time,"iso");
[t85.Year] = ymd(t85.Time);
[yearlyavg85.Year] = ymd(yearlyavg85.Time);
% ymd time , mean temperature for each year
t85 = join(t85,yearlyavg85,'Keys',{'Year'});
% GRAPH
figure(1)
plot(t85.Time,t85.Temp85_yearlyavg85,'-r','DisplayName','yearly_Temp_Rcp85'); % plot yearly temperature all over the year
%?????????????
%Plot the mean value of each year in one point 1st july only ??????
0 Comments
Accepted Answer
dpb
on 6 Jun 2022
Looks like a lot of extra work going on here--without the actual file we can't tell for sure, but one would presume the dates are in column A if the temperature data is column B...
Temperature85 = xlsread("filename.xlsx","Rcp85", "B3:B34700" ); %import data
writematrix(Temperature85, 'Temp85.txt');
Temp85= readmatrix("Temp85.txt");
year0_85=2006; % srtarting year
tT85=readtimetable("filename.xlsx",'Sheet',"Rcp85",'Range','A:B','NumHeaderLines',3,'ReadVariableNames',0);
tYA85=retime(tT85,'yearly','mean');
tYA85.Properties.VariableNames={'Year','AvgTemp'}; % set a known variable name
tYA85.Year=datetime(year(tYA85.Year),7,1); % set the wanted date in each year - retime uses first
plot(tYA85.Year,tYA85.AvgTemp,'-r','DisplayName','yearly_Temp_Rcp85');
Even if the guess about the file format isn't quite right, it's the general approach -- there's no need for all the intermediate steps; import the data directly however it is written.
The date for the average in the output file from retime is the first date in the time period by default; I don't recall having seen an option to change it. But, it's simple enough to just set a given month,day for the year as above. I just replaced the default time variable; you can add another if want/need both for some reason, of course.
0 Comments
More Answers (0)
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!