How to set a label on the xaxis

1 view (last 30 days)
I am trying to set a specific xlabel on a plot using set commands.
The graph plots wind data versus time of day and it goes like this:
TODAY = datenum([2012 9 21 7 0 0]);
REST_OF_TODAY = datenum([2012 9 21 17 0 0]);
y_max1 = max([5,10]);
y_min1 = min([5,10]);
y_max2 = max([6 11]);
y_min2 = min([6 11]);
plot(TODAY,y_max1,'-- ys','LineWidth',2,'MarkerEdgeColor','r','MarkerFaceColor','g','MarkerSize',10) hold on
plot(TODAY,y_min1,'--ys','LineWidth',2,'MarkerEdgeColor','b','MarkerFaceColor','y','MarkerSize',10)
plot(REST_OF_TODAY,y_max2,'--ys','LineWidth',2,'MarkerEdgeColor','r','MarkerFaceColor','g','MarkerSize',10) plot(REST_OF_TODAY,y_min2,'--ys','LineWidth',2,'MarkerEdgeColor','b','MarkerFaceColor','y','MarkerSize',10) set(gca,'XTick',TODAY:REST_OF_TODAY)
set(gca,'XTickLabel',{'Today','Rest of today'})
datetick('x',1) title('Wind(Knots) versus Time (dd-mmmm-yyyy)','fontsize',12,'fontweight','b');
xlabel('Time (dd-mmmm-yyyy)','fontweight','b');
ylabel('Wind (Knots)','fontweight','b');
grid minor
and I want to replace the datetick format number '1' by TODAY and REST OF TODAY as shown in this code:
t0 = 1
t1 = datenum([2012 9 21 7 0 0]) % 7 am
today t2 = datenum([2012 9 21 17 0 0]) % 5 pm
x = linspace(t1,t2); % generates a row vector y of 100 points linearly spaced between and including t1 and t2.
plot(x,sin(sqrt(x-t0)*pi)) set(gca,'XTick',[t1,floor(t1)+0.5],'XTickLabel',{'Today','Rest of today'})
grid minor
Can you please help?
  2 Comments
Star Strider
Star Strider on 27 Sep 2012
Edited: Star Strider on 27 Sep 2012
I do not fully understand what you want to do.
To start, I suggest deleting this line:
datetick('x',1) title('Wind(Knots) versus Time (dd-mmmm-yyyy)','fontsize',12,'fontweight','b');
and replacing it with this line:
title('Wind(Knots) versus Time (dd-mmmm-yyyy)','fontsize',12,'fontweight','b')
and run your code again. Does your code now do what you want it to do?
jjlivestrong
jjlivestrong on 27 Sep 2012
Yes. That it's exactly what I want to do, but Instead the code you are suggesting only outputs the name TODAY below the first points. I want to also display REST OF TODAY on the other points.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 27 Sep 2012
You only have two sets of points, so this is the best I can do:
TODAY = datenum([2012 9 21 7 0 0]);
REST_OF_TODAY = datenum([2012 9 21 17 0 0]);
y_max1 = max([5,10]);
y_min1 = min([5,10]);
y_max2 = max([6 11]);
y_min2 = min([6 11]);
plot(TODAY,y_max1,'-- ys','LineWidth',2,'MarkerEdgeColor','r','MarkerFaceColor','g','MarkerSize',10)
hold on
plot(TODAY,y_min1,'--ys','LineWidth',2,'MarkerEdgeColor','b','MarkerFaceColor','y','MarkerSize',10)
plot(REST_OF_TODAY,y_max2,'--ys','LineWidth',2,'MarkerEdgeColor','r','MarkerFaceColor','g','MarkerSize',10)
plot(REST_OF_TODAY,y_min2,'--ys','LineWidth',2,'MarkerEdgeColor','b','MarkerFaceColor','y','MarkerSize',10)
set(gca,'XTick',TODAY:REST_OF_TODAY)
set(gca,'XTickLabel',{'Today','Rest of today'})
title('Wind(Knots) versus Time (dd-mmmm-yyyy)','fontsize',12,'fontweight','b')
xlabel('Time (dd-mmmm-yyyy)','fontweight','b');
ylabel('Wind (Knots)','fontweight','b');
grid minor
t0 = 1
t1 = datenum([2012 9 21 7 0 0]) % 7 am today
t2 = datenum([2012 9 21 17 0 0]) % 5 pm
x = linspace(t1,t2); % generates a row vector y of 100 points linearly spaced between and including t1 and t2.
plot(x,sin(sqrt(x-t0)*pi))
% set(gca,'XTick',[t1,floor(t1)+0.5],'XTickLabel',{'Today','Rest of today'})
set(gca,'XTick',[t1 t2],'XTickLabel',{'Today','Rest of today'})
grid minor
Is that what you want? (I commented the line I replaced, and put the corrected line below it. I also included my earlier corrections to your code.)
  6 Comments
Moon Datta
Moon Datta on 15 Nov 2012
Thank you Sir. It has helped me also..........
Star Strider
Star Strider on 15 Nov 2012
Again, my pleasure!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!