How to change a single xtick (x label)?

7 views (last 30 days)
Dear Community
Here is the code on how i generated my figure:
x = 1:100;
y = rand (100);
plot(x,y)
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);
figure
plot(x,y)
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.
.
  4 Comments
Paul Hinze
Paul Hinze on 1 Feb 2023
i ran into a problem with this concept by using the compose command.
The attached picture shows the same question as before, but there the code does not work perfectly. The label of no stim should be shifted below the green errorbar by one index.
Here, is a snippet of a code i used:
xt = xticks;
% Fix xt
xt(end + 1) = depvars(2) - (depvars(3) - depvars(2)); % Concatenate vectors
xt = circshift(xt,1); % sort vector in risinf fashion
xticklabels([xt(1,{'No Stim'}) compose('%3d', xt(2:end))])
I tried to make the xt variable one index longer that it matches with the length and concept of the xticklabels, but somehow the code produces a faulty output. Can you help me?
Best, Paul
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;
figure
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)

Tags

Community Treasure Hunt

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

Start Hunting!