Question

Using the variables below, I need to plot data (x_values, y_values) on an x axis with ticks at ‘x_labels’ values. Each ‘x_labels’ tick must be evenly spaced on the x-axis (e.g. 1 cm).

I have been playing with 'XTick' and 'XTickLabel' variables and although I searched online all other examples are related to when the ‘x_labels’ and ‘x_values’ are identical. The challenge here is that there is different spacing between ‘x_labels’ and ‘x_values’. Please help!

x_labels = [4 8 16 32 64 128];
x_values = [5 10 35 50 60 70 90 120];
y_values = rand(1,length(x_values))

Thanks!

Était-ce utile?

La solution

Using the set command you can specify the xTick and xTickLabel (near the bottom of the link) properties of the axis separately, xTick defines the tick marks locations and xTickLabel defines the labels.
So after plotting the data:

plot(x_values,y_values);

we can freely set the locations, in this example they are spaced evenly between zero and the max of x_values. The set command is then be used to set the axis properties, (gca is the current axis handle)

x_label_locations = linspace(0,max(x_values),numel(x_labels));

set(gca,'xTick',x_label_locations,'xTickLabel',x_labels)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top