add text to value of a plot, part 2

2 views (last 30 days)
I have a plot with x axis takes the value January 1, January 2, .... and y axis takes the temperature of that day. In the x axis, I want to have these days and add a text "day" to these values so that they will be "Day 1 (January 1)","Day 2 (January 2)", "Day 3 (January 3)",... I did not find a relevant post on the Internet.

Accepted Answer

Star Strider
Star Strider on 26 May 2020
That needs a different approach.
Try this:
x = 1:10
y = rand(1,10);
figure
plot(x, y)
xt = get(gca, 'XTick');
xtklbl = sprintfc('Day %d (January %d)', [xt; xt]');
set(gca, 'Xtick',xt, 'XTickLabel',xtklbl, 'XTickLabelRotation',30)
You can also use the compose function in place of sprintfc (undocumented).
  9 Comments
Star Strider
Star Strider on 26 May 2020
As always, my pleasure!
I quite definitely agree that it is not an easy problem!

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!