# How can I solve this problem while plotting?

1 view (last 30 days)
Pul on 27 Aug 2021
Commented: Star Strider on 29 Aug 2021
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);
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Star Strider on 27 Aug 2021
Edited: Star Strider on 27 Aug 2021
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 CommentsShowHide 15 older comments
Star Strider on 29 Aug 2021
As always, my pleasure!
.

Sign in to comment.

### Categories

Find more on Time Series Events 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!