MATLAB Answers


X-axis data in years

Asked by Thomas
on 8 Dec 2012


I have an unemployment report that reports data by the month, my data has a total of 215 points, each point representing one month. I've been trying to figure out how to have the x axis simply display the data in terms of years but I have not had any luck, looking for some help. Basically I would like the x-axis to begin with 1995 and end with 2012.

This is what I have so far:

clear all ; close all ; set(0,'DefaultFigureWindowStyle','normal') ; drawnow ; clc ;

display('Importing data........'); unemployment = dataset('File', 'PE_Monthly.txt', 'delimiter', 'tab');

startdate = datenum('1995','yyyy') enddate = datenum('2012','yyyy') dt = linspace(startdate,enddate,12);

choice = menu('Unemployment Reports U3 vs. U6', '1995-2011', 'By year/month') if choice == 1 U6 = unemployment.U6RATE; U3 = unemployment.U3RATE; x = 1:215; plot (dt, U3, dt, U6);

I get some weird results on the x axis, nothing resembling 1995-2011.




No products are associated with this question.

1 Answer

Answer by the cyclist
on 8 Dec 2012
Edited by the cyclist
on 8 Dec 2012
 Accepted answer

It is presumably plotting the datenums, just as you told it to. ;-)

Try using the datetick() function after you plot.


EDIT: @Thomas, I moved your comment here, rather than having it listed as another "answer", where you had placed it.]

Thank you worked like a charm, one thing though it did in increments of 3, is there any way to force it to display each year? I think my problem was adding datetick after the plot function.

Before you do the datetick command, you should be able to use the command

> set(gca,'XTick',<put datenums here>)

to set the ticks exactly where you want them (for example at the year marks). Then, when you use datetick() function, be sure to use the 'keepticks' option.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!