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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

plot data instead of number on the xaxis

Asked by Locks on 18 May 2013

Hi, I have the following problem: I would like to plot two time series in the same graph which is working fine, but I would like to have displayed the data (here the respective vector is also called date) in a format such as: 01/01/2010 The plot should show the first day of each months so 01/01/2001 01/02/2001 and so on

the code I am using at the moment ist the following one:

filename = 'SPXvsVIX.xlsx';
sheet=2;%3 
xlRange='C7:C3402';
date=xlsread(filename, sheet, xlRange);
xlRange='D7:D3402';
SPX=xlsread(filename, sheet, xlRange);
xlRange='I7:I3402';
VIX=xlsread(filename, sheet, xlRange);
dateMatlab=date+693960;
dataSet=[dateMatlab,SPX,VIX];
%enter period you will look at
%start date:
DateString='05-Aug-2002';
%DateString='01-Jan-2001';
StartDate=datenum(DateString);
%end date
DateString='30-Jan-2003';
%DateString='31-Dec-2001'
EndDate=datenum(DateString);
%returns a data matrix consisting only of those datas which are between
%start and end date
dataSet = dataSet(dataSet(:,1)>=StartDate & dataSet(:,1)<=EndDate, :) ;
dates=datenum(dataSet(:,1));
%SPX
SPX=dataSet(:,2);
%VIX
VIX=dataSet(:,3);
plotyy(dates,SPX,dates,VIX);

0 Comments

Locks

Products

No products are associated with this question.

1 Answer

Answer by per isakson on 18 May 2013
Edited by per isakson on 18 May 2013
Accepted answer

You need to assign date string values to the axes' property, XTickLabel. On-line help: For example, the statement:

    set( gca, 'XTickLabel', {'One';'Two';'Three';'Four'} ) 

The width of the date strings might become a problem. The property XTick controls number and position of the xtick labels.

.

Run this example

    %%
    sdn             = [ 1 : 6 ] + 735370;
    date_strings    = datestr( sdn, 'dd-mmm-yyyy' );
    %%
    plot( [1:6] )
    axh = gca;
    %%
    set( axh, 'XTickLabel', date_strings ) 

3 Comments

Locks on 18 May 2013

what are one, two three and four doing?

I do not necessarily need the code above, I am just looking for a way to plot the dates instead of numbers on the a x-axis and only the first date of the months

per isakson on 18 May 2013

'One', 'Two' and 'Three' serve as examples of string values

"not necessarily need the code above" AFAIK: there is no other way to replace the numbers by text

See example above

Locks on 18 May 2013

ok thanks.

per isakson

Contact us