How to generate quarterly date in Matlab?

65 views (last 30 days)
Niraj Prasad Koirala
Niraj Prasad Koirala on 5 Jul 2020
Answered: dpb on 6 Jul 2020
I need to plot timeseries plot of different macroeconomic variables from 1966Q2 to 2020Q1. I used the code ts= 1966Q1: 2020Q1. But I get bugging all the time.
  2 Comments
dpb
dpb on 6 Jul 2020
Post actual code/error, not just description.
'1996Q1' is not a valid variable name nor a valid date...you could use it as an input string to create one as:
>> datetime('1966Q1','InputFormat','yyyy''Q''Q')
ans =
datetime
01-Jan-1966
>>
Niraj Prasad Koirala
Niraj Prasad Koirala on 6 Jul 2020
Edited: dpb on 6 Jul 2020
Hi thanks for your suggestion.
I used the following code:
ea = muja(:,1); %data file
state= muja(:,2);
t1 = datetime(1966,4,1);
t2 = datetime(2020,1,1);
t = t1 + calquarters(1:54*4)
numPoints = length(t)
subplot1 = subplot(3,3,1,'Parent',figure);
hold(subplot1,'on');
plot(t,ea,'b-','linewidth',2);
grid on;
xlabel('Date', 'FontSize', fontSize);
ylabel('ea', 'FontSize', fontSize, 'Interpreter', 'none');
datetick('x', 'mm/dd/yyyy');
ax = gca;
ax.XTickLabelRotation = 90;
I got following message:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
I am trying to plot someting like the file attached to my response.
Thanks once again for your time and help.

Sign in to comment.

Answers (1)

dpb
dpb on 6 Jul 2020
datetick('x', 'mm/dd/yyyy');
is your problem. datetick is/was ONLY used for the deprecated datenum which was just a double in disguise.
When you plot() with datetime and/or duration variables, the x-axis is a 'DatetimeRuler' object.
You set the date format with it using the same formatting strings as datetime uses.
At
ax.XAxis
you'll get the handle to the XAxis object and can see all the properties for it...there's also a doc page for the plot() function for plotting datetime data.

Community Treasure Hunt

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

Start Hunting!