Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a specific number of tick marks on MATLAB plot

Tags:

plot

matlab

I am trying to figure out how to set a custom number of tick marks on the x-axis of a plot I am creating in MATLAB, but I am not sure if this is possible. I saw this question that seems to be asking the same thing, but that answer wasn't helpful since

set(gca,'XTick',tickVector);

sets the location of the ticks, not the number of ticks. My code is as follows.

rangeBegin = 100000;
rangeEnd = 200000;
numberOfXTicks = 5;

plot(data(:, 1));
xAxisVals = linspace(rangeBegin, rangeEnd, numberOfXTicks);
%set(gca,'XTick',rangeBegin:rangeEnd); % Doesn't work as expected
set(gca,'XTickLabel',xAxisVals);

So in this example, I am just looking for a way to force MATLAB to create the plot with 5 ticks on the x-axis in order to match the 5 XTickLabels that I have set.

data is an array of doubles that is roughly <3000x1>.

EDIT: I should also add that I want my x-axis values to be from a separate array. The data array shown above corresponds to a time array (not shown...my bad), and each value in the data array has a corresponding value in the time array. Since I am selecting a range from the data array, I want to select the corresponding time values and use those as the x labels. But obviously I do not want 3000 time labels on my x-axis. Hopefully this is more clear.

like image 294
Absolute Zero Avatar asked Mar 19 '14 20:03

Absolute Zero


People also ask

How do I change the number of ticks in Matlab?

MATLAB selects the tick-mark locations based on the range of data so as to produce equally spaced ticks (for linear graphs). You can specify different tick marks by setting the axes XTick and YTick properties. Define tick marks as a vector of increasing values. The values do not need to be equally spaced.

How do I specify a tick in Matlab?

Change Tick Value Locations and LabelsCreate y as the cosine of x. Plot the data. Change the tick value locations along the x-axis and y-axis. Specify the locations as a vector of increasing values.

How do you set Y-axis ticks?

Specify y-Axis Tick Values for Specific Axes Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes. Set the y-axis ticks for the lower plot by passing ax2 as the first input argument to the yticks function.

How do you add a tick mark to a graph?

Add tick marks on an axisClick the chart, and then click the Chart Design tab. Click Add Chart Element > Axes > More Axis Options. On the Format Axis pane, expand Tick Marks, and then click options for major and minor tick mark types.


1 Answers

numberOfXTicks = 5;

h = plot(data(:, 1));
xData = get(h,'XData');
set(gca,'Xtick',linspace(xData(1),xData(end),numberOfXTicks))
like image 165
Luis Mendo Avatar answered Sep 20 '22 18:09

Luis Mendo