# Matlab figure: axis re-labelling

• MATLAB
Hi all.

i am integrating in matlab and plotting the results in a y-verically and t-horizontally. Matlab automatically plots y versus the time step which is great. but what i need to do is re-label the t-axis so the actual time appears instead of the time step- so instead of from 0 to 1200, it would be 0-5. Any idea how to do that?

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
This may be a bit out of leftfield, but have you tried

Code:
doc plot

I did... and tried this
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})

but could not figure out a way for the conversion... here are more specifics...
i am running the integration for t=1 and time step dt=.0063 which makes the total number of n around 160. so when the figure is plotted it is plotted from 0 to 160 (the n's) but instead i want it to show from 0 to 1 with increments of .1

for these specific parameters i did the following and it is now working out and not sure why:
set(gca,'XTick',0:.1:1)
set(gca,'XTickLabel',{'.1','.2','0.3','.4','.5'})

also, i often chance t, so is there a way to have matlab calculate it for me so i don't have to hardwire with every change in t.

i hope this clarifies my confusion :)

What command are you using to plot it?

plot(y);
will plot it by index number.

plot(t, y);
will plot y versus t.

If you want t to be 0 to 1 in increments of 0.0063, then
t=0:0.0063:1;

this is what i am using plot(y);
and it is plotting y vs. t. the plotting itself is not the problem...
i simply want the t-axis to be re-labeled... instead of the tick marks showing from 0 to 160, i want the change to be only in the tick marks and want it to show from 0 to 1 with increments of 0.1. as i said, the graph is fine, i just want the numbers that show on the figure to be different

But did you try plot(t, y)? That will label the plot automatically.

Using just plot(y) is not plotting it versus time, it is plotting it versus index number, your 1 to 160 that you are seeing. As long as the data you are plotting is evenly spaced, using just plot(y) is ok. However, if you have a data set like this:

t=[ 0 1 2 5 20]
y=[ 1 2 3 4 5]

will produce very different plots using

In this case the plot(y) will show the wrong picture. plot(t,y) will show the right one.

But if really insist on doing the labels manually, then use
set(gca,'XTick',0:.1:1)
set(gca,'XTickLabel',[0:.1:1])

Last edited:
Ok, I had a chance to check this out with MATLAB at work and I see where the problem is. Here is what you can do if you want to label the axis yourself. If you use plot(y), then as I said before, MATLAB is plotting y against the index number. So for the case that you gave in your first post, lets say that MATLAB is plotting 0 to 1200. You need to tell MATLAB where to put tick marks USING THIS SCALE. So if you want 10 equal spaces, then this is a delta of 1200/10 = 120. So you need to tell MATLAB to put a tick mark at 0, 120, 240, etc by

set(gca, 'XTick', 0:120:1200);

Now you need to setup the labels you want to put there. If you want 0 to 5, then with 10 spaces, you want the labels every 5/10, or 0.5. Tell matlab to make labels of 0, 0.5, 1.0, etc by

set(gca, 'XTickLabel', [ 0:0.5:5 ] );

To do this automatically, use
xlim=get(gca, 'XLim');
to put the actual plot limits in the variable xlim, xlim(1) is the lower bound, xlim(2) is the upper bound. In our example this would be 0 and 1200.

Do the math using xlim to get the tick spacing you want. Then use the above set commands with your calculated new tick mark positions.

Just tried it. Worked perfectly. Thanks a lot Chrisas.