How to change a single xtick (x label)?

4 views (last 30 days)
Dear Community
Here is the code on how i generated my figure:
x = 1:100;
y = rand (100);
I would like to change the zero label on the x-axis with 'no stim', at the same time ALL other values need to remain unchanged. Furthermore they need to stay flexible (Without magic numbers). Do you know how to do that?

Accepted Answer

Star Strider
Star Strider on 30 Jan 2023
Perhaps something like this —
x = 1:100;
y = rand (100);
xt = xticks;
xticklabels([{'No Stim'} compose('%3d',xt(2:end))])
This simply concatenates {'No Stim'} with the cell array created by compose to create the rest of the cell array defining the rest of the xticklabels.
Star Strider
Star Strider on 1 Feb 2023
I get the impression that you are plotting the black line (and what appears to be a 'pchip' interpolant) first, and then the green value and errorbar separately. (I can’t follow the code snippet.)
It would really help to have your data. Lacking it, I did my best to emulate it, so that porting it to your data would be relatively straightforward —
x = 9000:250:12500;
y = (rand(size(x))+0.25)*100;
err = rand(size(x))*25;
errorbar(x+500, y, err, '-k')
xt = xticks; % Get Original 'xticks' Vector
xlim([min(xt)-1000 max(xt)+500]) % Set X-Limits
xticks([9000 xt]) % Re-Define 'xticks' Vector
xticklabels([{'No Stim'} compose('%d',xt(1:end)-500)]) % Set 'xticklabels'
hold on
errorbar(9000, rand*100, rand*25, '-g', 'LineWidth',1.5) % Plot Green Errorbar
hold off
You may still have to tweak this to get it the way you want. This should get you started.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by