Bazman
Bazman

Reputation: 2150

Matlab plotting with Dates

The code below works fine as is, however I need to be able to change the start and end dates to be any given calendar date. So rather than run from 2002:2003 I would like to have it run from '15/Aug/2001' to '14/Aug/2002'. In addition I would like the y-axis to display these new dates.

The actual number of days between then will not be the same as calendar says but rather 255 days (in this case) I will have to enter the information manually.

It seems to be datetick can help but I cannot get it to fit into the framework below:

I know I've already asked a few questions on this piece of code but this will be the last one!

tchange=(1/(255-1));
x = 2002:tchange:2003; % x data
grad_ = rand(1,length(x))*.3; % graduated stuff
grad_2 = rand(1,length(x))*.3;
grad_3= rand(1,length(x))*.3;
h = subplot(1,3,1);
%plot(grad_,x); % flip x and y for vertical plot
herrorbar(grad_,x,grad_2,grad_3,'.');
axis(h, [0 0.5 2002 2003])
set(h, 'Ytick', x(1):x(end), 'Xtick', 0:.1:.5, 'YDir','reverse', 'YGrid', 'on');
xlabel('Gradient Search')

diff_ = rand(1,length(x)).^2 *.15; % differential stuff
h = subplot(1,3,2);
herrorbar(diff_,x,grad_2,grad_3,'.');
%plot(diff_,x);
set(h,'yticklabel',[], 'Ytick', x(1):x(end), 'Xtick', 0:.1:.5, 'YDir','reverse', 'YGrid', 'on');
axis(h, [0 0.5 2002 2003])
xlabel('Differential Evolution')

delta_ = rand(1,length(x)).^2 *.2 - .2; % delta stuff
h = subplot(1,3,3);
%plot(delta_,x);
stem(x,delta_median_LP(1:npoints,1),'Marker','.');
axis(h, [2002 2003 -.5 .5])
set(h,'Xtick', -.5:.5:.5, 'XGrid', 'on');
view(90,90);
ylabel('\Delta of medians')

Upvotes: 0

Views: 319

Answers (1)

MrAzzaman
MrAzzaman

Reputation: 4768

You should be able to use MATLAB's datenum function, combined with the datetick function. For instance, for the first plot, you could do the following:

x = datenum(2001,8,15):datenum(2002,8,14); % x data
grad_ = rand(1,length(x))*.3; % graduated stuff
grad_2 = rand(1,length(x))*.3;
grad_3= rand(1,length(x))*.3;
h = subplot(1,3,1);
plot(grad_,x); % flip x and y for vertical plot
% herrorbar(grad_,x,grad_2,grad_3,'.');
axis(h, [0 0.5 x(1) x(end)])
set(h, 'Xtick', 0:.1:.5, 'YDir','reverse', 'YGrid', 'on');
datetick('y',24,'keeplimits');
xlabel('Gradient Search')

I remove the ytick part of the set command so that it uses the default, which isn't too bad in this case, but you could set it to whatever you wanted.

Upvotes: 1

Related Questions