How can I solve this problem while plotting?

Hello everyone,
thanks to a member of this community, I found a way to do the differences between years. The problem is that when I go to plot, I don't obtain differences between 15 Dec 1998-7 Dec 1999 (for istance) but but differences between 1 Jan 1998-1 Jan 1999.
Can anyone help me please?
Thank you very much.
clear all
close all
LD = load('giulia_TT.mat');
giulia_TT = LD.giulia_TT;
GIULIA_YEARLY_N = retime(giulia_TT,'yearly', @(x)mean(x,1,'omitnan')); % Yearly Average
N=GIULIA_YEARLY_N.Var5*3.5;
SelIdx = year(GIULIA_YEARLY_N.Time) >= 1998; % Logical Mask Vectror
N_Sel = N(SelIdx);
N_Sel_Dif = -diff(N_Sel);
Years = 1998+(1:numel(N_Sel_Dif));
limits_mtx = datetime({'15 Dec 1998', '07 Dec 1999'; '07 Dec 1999', '30 Nov 2000';'30 Nov 2000','13 Nov 2001';'13 Nov 2001','30 Dec 2002';'30 Dec 2002','4 Jan 2004';'4 Jan 2004','22 Nov 2004';'22 Nov 2004','24 Nov 2005';'24 Nov 2005','15 Dec 2006';'15 Dec 2006','22 Jan 2008'; '22 Jan 2008','9 Jan 2011';'1 Jan 2010','16 Nov 2011';'6 Nov 2011','9 Jan 2013';'9 Jan 2013','5 Jan 2014';'5 Jan 2014','31 Jan 2015';'31 Jan 2015','25 Jan 2018';}, 'InputFormat','dd MMM yyyy')
Years = datetime(Years, 1, 1, 'Format', 'yyyy');
figure
plot(Years, N_Sel_Dif)
grid
xlim([min(Years) max(Years)])
xlabel('Years')
ylabel('Yearly N-Differences')
set(gca, 'XTick',Years)
plot(Years, limits_mtx,'g-^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
hold on
set(gca, 'ytick', -200:25:400);

 Accepted Answer

I do not understand what you are doing.
This call throws an error:
plot(Years, limits_mtx,'g-^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
because the first two arguments have no common dimensions.
I have no idea what you want to do, so try this to see if it is close.
Change it if it is not:
LD = load('giulia_TT[1].mat');
giulia_TT = LD.giulia_TT;
GIULIA_YEARLY_N = retime(giulia_TT,'yearly', @(x)mean(x,1,'omitnan')); % Yearly Average
N=GIULIA_YEARLY_N.Var5*3.5;
SelIdx = year(GIULIA_YEARLY_N.Time) >= 1998; % Logical Mask Vector
N_Sel = N(SelIdx);
N_Sel_Dif = -diff(N_Sel);
Years = 1998+(1:numel(N_Sel_Dif));
GIULIA_DAILY_N = retime(giulia_TT,'daily', @(x)mean(x,1,'omitnan')); % Daily Means
limits_mtx = datetime({'15 Dec 1998', '07 Dec 1999'; '07 Dec 1999', '30 Nov 2000';'30 Nov 2000','13 Nov 2001';'13 Nov 2001','30 Dec 2002';'30 Dec 2002','4 Jan 2004';'4 Jan 2004','22 Nov 2004';'22 Nov 2004','24 Nov 2005';'24 Nov 2005','15 Dec 2006';'15 Dec 2006','22 Jan 2008'; '22 Jan 2008','9 Jan 2011';'1 Jan 2010','16 Nov 2011';'6 Nov 2011','9 Jan 2013';'9 Jan 2013','5 Jan 2014';'5 Jan 2014','31 Jan 2015';'31 Jan 2015','25 Jan 2018';}, 'InputFormat','dd MMM yyyy');
Edges = [limits_mtx(:,1); limits_mtx(end,2)].';
for k = 1:numel(Edges)-1
idxrng = (GIULIA_DAILY_N.Time>=Edges(k)) & (GIULIA_DAILY_N.Time<Edges(k+1)); % Date Range
if (k+1) == numel(Edges)
idxrng = (GIULIA_DAILY_N.Time>=Edges(k)) & (GIULIA_DAILY_N.Time<=Edges(k+1)); % Date Range (Last Bin)
end
limits_counts(k) = mean(GIULIA_DAILY_N{idxrng,6},'omitnan'); % Mean Of Bin Range
end
% limits_counts
Years = datetime(Years, 1, 1, 'Format', 'yyyy');
figure
plot(Years, N_Sel_Dif)
grid
xlim([min(Years) max(Years)])
xlabel('Years')
ylabel('Yearly N-Differences')
set(gca, 'XTick',Years)
figure
plot(median(limits_mtx,2), limits_counts, 'g-^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
hold on
hold off
grid
set(gca, 'XTick',median(limits_mtx,2))
xtickformat('yyyy')
xlabel('Bin (Median Year)')
ylabel('Bin Mean')
% set(gca, 'ytick', -200:25:400);
Make appropriate changes to get the result you want.
EDIT — (27 Aug 2021 at 14:44)
To get differences between those specific years —
for k = 1:numel(Edges)-1
idxrng = (GIULIA_DAILY_N.Time>=Edges(k)) & (GIULIA_DAILY_N.Time<Edges(k+1)); % Date Range
if (k+1) == numel(Edges)
idxrng = (GIULIA_DAILY_N.Time>=Edges(k)) & (GIULIA_DAILY_N.Time<=Edges(k+1)); % Date Range (Last Bin)
end
idxrng = find(idxrng);
limits_dif(k) = diff(GIULIA_DAILY_N{idxrng([1 end]),6}); % Mean Of Bin Range
end
figure
plot(limits_mtx(:,2), limits_dif, 'g-^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
hold on
hold off
grid
set(gca, 'XTick',median(limits_mtx,2))
xtickformat('yyyy')
xlabel('Bin (Median Year)')
ylabel('Bin Ends Difference')
ylim([-1 1]*100)
text(limits_mtx(:,2), limits_dif, compose(' \\leftarrow %s', limits_mtx(:,2)), 'Horiz','left', 'Vert','middle', 'Rotation',80, 'FontSize',8)
Changing the plot call to:
plot(limits_mtx(:,2), limits_dif, 'g-^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
and adding the text call, the dates all appear to be correct when I run it.
Experiment to get the result you want.
.

16 Comments

@Star Strider Thank you.
Actually, I would like to the differences between these years:
limits_mtx = datetime({'15 Dec 1998', '07 Dec 1999'; '07 Dec 1999', '30 Nov 2000';'30 Nov 2000','13 Nov 2001';'13 Nov 2001','30 Dec 2002';'30 Dec 2002','4 Jan 2004';'4 Jan 2004','22 Nov 2004';'22 Nov 2004','24 Nov 2005';'24 Nov 2005','15 Dec 2006';'15 Dec 2006','22 Jan 2008'; '22 Jan 2008','9 Jan 2011';'1 Jan 2010','16 Nov 2011';'6 Nov 2011','9 Jan 2013';'9 Jan 2013','5 Jan 2014';'5 Jan 2014','31 Jan 2015';'31 Jan 2015','25 Jan 2018';}, 'InputFormat','dd MMM yyyy');
Shorltly, calculating the Delta between these years 15 Dec 1998-7Dec1999, 7 Dic 1999-30 Nov 2000 and so on!
I don't know if I was clear.
My pleasure!
Not quite.
See my edited version (at the end of my Answer).
.
Pul
Pul on 27 Aug 2021
Edited: Pul on 27 Aug 2021
@Star StriderThanks for your patience.
Sorry for my explanation, I try another time:
In this plot, the delta was made considering 1 Jan 1998-1 Jan 1999 ; 1 Jan 1999-1 Jan 2000.
Now, I would like to do the delta between the years you can see in "limits_mtx", because I have to compare these data with other data that have the same time ranges, that it's not exactly 365 days (like for the above plot, in which I did the delta between 1 Jan 1998-1 Jan 1999 ; 1Jan 1999-1 Jan 2000 and so on...so considering 365 days) but, for instance,357 days for this time range 15 Dec 1998'-'07 Dec 1999'.
P.S. When I see the green plot, data don't seem to correspond to data in limits_mtx: eg the first green triangle has 11 Jun 1999 and not 7 Dec 1999!
My pleasure!
My code calculates the difference as best I can considering that I am not certain (since I do not understand) what result you want.
I set the x-ticks to the median of the intervals. Set them to whatever you, so want. So if you want them to be the second column in ‘limits_mtx’:
set(gca, 'XTick',limits_mtx(:,2))
My code calculates the differences between the beginning and end times of the intervals in ‘limits_mtx’ , since that is what you requested. Each of the intervals will have different numbers of days if that is how you defined them. That is not a problem.
.
Yes, I saw you used the data I asked, but I can't understand why the data, in the plot, are different and why there's a gap in 2013! (In the picture below, you can see that these triangles have different datatime (11 Jun 1999 instead of 7 Dec 1999; 3 Jun 2000 instead of '30 Nov 2000 ; 23 May 2001 instead of '13 Nov 2001'".
Thank you very much!
The gap is because the dates defining those edges are not continuous. The other intervals all share an edge.
With respect to the dates in the plot, I have no idea why you are experiencing that problem.
See the latest EDIT to my Answer.
.
@Star StriderThank you very much,now it works!
It's just I don't get why there's a gap given that there's the edge in 2014, as you can see...
('9 Jan 2013','5 Jan 2014';'5 Jan 2014','31 Jan 2015';)
All the other segments share an edge, so while the plot appears to be continuous, it actually is not.
Each segment is plotted independently.
The gap appears where the segments do not share an edge. It would be necessary to use the hold function, and then another plot call to fill that line, if necessary:
plot(limits_mtx(end-1:end-1,2), limits_dif(end-2:end-1), 'g--^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
I did not test that, however it should work. (I added a dashed line rather than a solid line, in case it is necessary to emphasize that the segments are not connected. Change that back if you want.)
.
Pul
Pul on 27 Aug 2021
Edited: Pul on 27 Aug 2021
Yes, but my question is why the triangle ( so the data) doesn't appear in 2014 although there's a value for 2014?
Moreover, which data are taking into account for the plot?
I needed to plot the variable "N" with the differences between years (delta) you did it, not using daily means but just yearly means!
I'm really sorry for all of these questions, but 'm getting confusing!
Yes, but my question is why the triangle ( so the data) doesn't appear in 2014 although there's a value for 2014?
When I looked at the data, that value was NaN, and NaN values do not plot. That explains the discrepancy in the plotted results. That likely also explains the gap.
.
Yes, I saw there's a NaN but I was wondering why it went out "NaN" although there are data available.
So you're code is taking into account "GIULIA_DAILY" (so daily means) and not "N" with yearly means, right?
Because I'm trying to change GIULIA_DAILY here, with GIULIA_YEARLY_N (I replaced VAr5 with "variable N", but it doesn't work.
for k = 1:numel(Edges)-1
idxrng = (GIULIA_YEARLY_N.Time>=Edges(k)) & (GIULIA_YEARLY_N.Time<Edges(k+1)); % Date Range
if (k+1) == numel(Edges)
idxrng = (GIULIA_YEARLY_N.Time>=Edges(k)) & (GIULIA_YEARLY_N.Time<=Edges(k+1)); % Date Range (Last Bin)
end
idxrng = find(idxrng);
limits_dif(k) = diff(GIULIA_YEARLY_N{idxrng([1 end]),6}); % Mean Of Bin Range
end
Thank you very much.
As always, my pleasure!
Yes, I saw there's a NaN but I was wondering why it went out "NaN" although there are data available.
Even though I set 'omitnan', any missing data would still produce a NaN result.
So you're code is taking into account "GIULIA_DAILY" (so daily means) and not "N" with yearly means, right?
If I remember correctly, ‘N’ is ‘Var5’ multiplied by a constant, so the daily mean would be computed the same way — multiply ‘Var5’ by the same constant.
Because I'm trying to change GIULIA_DAILY here, with GIULIA_YEARLY_N (I replaced VAr5 with "variable N", but it doesn't work.
It doesn’t work because ‘Edges’ (and any parts of my code that use that array) are configured to work with daily data. Create ‘GIULIA_DAILY_N’ and my code will work with it.
My apologies for the delay — sleeping, then breakfast.
.
Pul
Pul on 28 Aug 2021
Edited: Pul on 28 Aug 2021
Don't worry, thanks for answering to all of my questions!
So you got it the yearly average doing the daily average before?
Because I thought to calculate the yearly average directly, not the daily average and then the yearly one!
As always, my pleasure!
So you got it the yearly average doing the daily average before?
I’m not certain what you’re referring to. I had to use the daily averages to calculate the intervals using ‘Edges’ (that were across year boundaries). The retime function calculates using calendar days, calendar months, calendar years (or a few other types of aggregation), and it’s not possible to re-define those limits, to the best of my knowledge. So to aggregate with different limits, the sort of procedure I used is necessary.
.
Got it.
Thank you very much!
As always, my pleasure!
.

Sign in to comment.

More Answers (0)

Categories

Asked:

Pul
on 27 Aug 2021

Commented:

on 29 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!