How to 1:1 map axis entries in a simple plot to another vector?

5 visualizaciones (últimos 30 días)
Distelfink
Distelfink el 14 de Mzo. de 2022
Respondida: Piyush Patil el 6 de Oct. de 2023
Dear matlab forum,
Please consider the following case:
v=1:10;
plot(1:10,v(1,:))
I would like to 1:1 map the entries on the horizontal axis in the figure to another vector, e.g. w=71:80. That is, I want the vector entries of w displayed on the axis of the figure, but keeping the same plot.
I could of course do v2=[zeros(1,70) v]
v2=[zeros(1,70) v];
plot(71:80,v2(1,71:80))
But is there another way, without adding the zeros and creating a new vector?
Thank you very much in advance
P.S.: for some reason I cannot run the code - is this a general problem at the moment in the matlab forum?

Respuestas (1)

Piyush Patil
Piyush Patil el 6 de Oct. de 2023
Hello,
As per my understanding, you would like to perform 1 on 1 mapping of the entries on the horizontal axis of the figure to another vector (say "w") such that entries of vector "w" should be displayed on figure but keeping the plot same. And you want to perform this task without adding the zeros and creating new vector.
There is a way to achieve the desired mapping without creating new vector by using xticks function in MATLAB. This function allows you to specify the positions of the tick marks on x-axis.
Here is an example showing how you can do it:
v = 1:10;
w = 71:80;
plot(1:10, v(1,:))
% Set the x-tick positions and labels
xticks(1:numel(w))
xticklabels(w)
% Adjust the x-axis limits
xlim([1, numel(w)])
% Keep the existing plot
hold on
% Plot a vertical line to indicate the mapping
line([1, 1], ylim, 'Color', 'green')
% Release the hold
hold off
I hope this resolves the issue you are facing.

Categorías

Más información sobre Graphics Object Properties en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by