# Matlab plotting function

Hi Professor

I am have a bit of trouble plotting data with matlab …
The data is quartely, and starts from 1986Q4 to 2016Q4,
and I want to specify time axis with Matlab…

clear all; close all; clc;

k=0;

for i=1986:2016;
if i==1986
quarters=;
else
quarters=[3 6 9 12];
end

``````    for j=quarters
k=k+1;
T(k,1)=datenum(i,j,1);
end
``````

end

plot(T,a1,‘b’,‘LineWidth’,2);
set(gca,‘XTick’,[T(1):365*1:T(end)]);
datetick(‘x’,‘yy’,‘keepticks’);
recessionplot;
ax = gca;
ax.YAxis.Exponent=0;
set(gca,‘FontSize’,14, ‘FontName’,‘Times’);

However, when specifying dates as shown below in “test.m”, the plot I got isn’t exactly correct.

From the plot below, it looks like the data starts from somewhere in 86 and ends in 15Q4….
Please could you indicate me the problem??

Q2. the “recessionplot” build-in function with Matlab is based on the US data. However, if I want to customize it for the EU data or UK data, how am I suppossed to mofify the “recessionplot” part?

say, if I want to specify the recession for the UK as 2008Q1-2009Q1, how am I supposed to modify my clode?

a1.csv (1.1 KB) test.m (456 Bytes)

1. This is due to Matlab changing the xlim. You need to preserve the limits. The following works:
``````clear all; close all; clc;
T=size(a1,1);

dates=datenum([1986;kron([1987:2016]',ones(4,1))],[10;repmat([1:3:10]',30,1)],ones(T,1));

figure;
plot(dates,a1,'b','LineWidth',2);
hh=gca;
axis tight
set(gca,'Xtick',dates(2:4:end-4))
set(gca,'XtickLabel',dates(2:4:end-4))
datetick('x','yy','keepticks','keeplimits');
recessionplot;
ax = gca;
ax.YAxis.Exponent=0;
set(gca,'FontSize',14, 'FontName','Times');`
``````
1. The dates of recessions can be passed using the `'recessions' — Recession data` name/value pair of the function. See the Matlab help.

Professor,
Q1, the code gave me:

with the datatick of last year 2016 missing (data spans 1986Q4-2016Q4)

when I tried to modify “set(gca,‘XtickLabel’,dates(2:4:end-4))” to “set(gca,‘XtickLabel’,dates(2:4:end))”, it gave me:

the duration of the year 2016 was shorter than those of previous years, like 2015…

so I think there might be some problems…

besides, if I want to plot the time interval as 2 years, how am I supposed to modify the code?
I tried to modify “set(gca,‘Xtick’,dates(2:4:end-4))
set(gca,‘XtickLabel’,dates(2:4:end-4))” to "set(gca,‘Xtick’,dates(2:8:end-4))
set(gca,‘XtickLabel’,dates(2:8:end-4)), but only got:

Of course 2016 it is shorter, because the data ends before 2017. That is also the reason it is not shown.

1 Like

hi professor,

In Small-Open-Economy framework where domestic households have access to both domestic (B) and foreign bonds ( B_*).

would the debt/gdp ratio be defined as B/Y?

That question is not well-defined. Which debt/GDP ratio are you referring to? Net or gross? Private or public? External or total?

I was referring to public debt, assuming that the households have access to both domestic and foreign bonds.
Thanks

If you consider total public debt and the government issues that debt domestically and internationally, you want to have internal and external debt. So you need to add B and B^* (and potentially transform them to the same units)

1 Like

Hi Professor,

further to my previous question,
If I want to plot every 3 years on x-axis (data label), e.g. 1987,1990,1993,1996…how am I supposed to modify the codes below:

clear all; close all; clc;
T=size(a1,1);

dates=datenum([1986;kron([1987:2016]’,ones(4,1))],[10;repmat([1:3:10]’,30,1)],ones(T,1));

figure;
plot(dates,a1,‘b’,‘LineWidth’,2);
hh=gca;
axis tight
set(gca,‘Xtick’,dates(2:4:end-4))
set(gca,‘XtickLabel’,dates(2:4:end-4))
datetick(‘x’,‘yy’,‘keepticks’,‘keeplimits’);
recessionplot;
ax = gca;
ax.YAxis.Exponent=0;
set(gca,‘FontSize’,14, ‘FontName’,‘Times’);

Then you need to use steps of 12 quarters:

``````set(gca,'Xtick',dates(2:12:end))
set(gca,'XtickLabel',dates(2:12:end))
``````

Please could you also indicate me :

Q1. if the data spans 1986Q4 to 2016Q3, then how should I mofidy the part below?

dates=datenum([1986;kron([1987:2016]’,ones(4,1))],[10;repmat([1:3:10]’,30,1)],ones(T,1));
set(gca,‘Xtick’,dates(2:4:end-4))
set(gca,‘XtickLabel’,dates(2:4:end-4))

Q2. what if the data spans: 1986Q1 to 2016Q4？

Q3. What if the data are monthly?

Thanks!

The part in the brackets

``````dates=datenum([1986;kron([1987:2016]’,ones(4,1))],[10;repmat([1:3:10]’,30,1)],ones(T,1));
``````

creates a date number of the format YYYY,MM,DD. The first part is about the year. Here, we start with 1986. In the month part, we start with month 10, i.e. quarter 4. The `kron([1987:2016]’,ones(4,1))` part repeats the years 1987 to 2016 four times as we are matching it to the 4 months indicating the respective quarters. So if you want 1986Q4 to 2016Q3:

``````T=length(1986.75:0.25:2016.5)'
dates=datenum([1986;kron([1987:2015]',ones(4,1)); 2016; 2016; 2016],[10;repmat([1:3:10]',29,1); 1;4;7],ones(T,1));
``````

while it is for 1986Q4 to 2016Q4

``````T=length(1986:0.25:2016.75)'
dates=datenum([kron([1986:2016]',ones(4,1))],[repmat([1:3:10]',31,1)],ones(T,1));
``````

Monthly is similar

``````T=length(1986:1/12:2016+11/12)'
dates=datenum([kron([1986:2016]',ones(12,1))],[repmat([1:12]',31,1)],ones(T,1));
``````
1 Like

is there a typo in your reply? as it should be “while it is for 1986Q1 to 2016Q4”, right?

Please could you explain a bit why did you change [10;repmat([1:3:10]’,30,1)] to [10;repmat([1:3:10]’,29,1)] and [10;repmat([1:3:10]’,31,1)] ?

The number 29 or 30 denotes the number of full years in the middle where all three quarters are present.

1 Like