In Matlab: How to keep all xTicks but xTicklabels just on every 6 xTicks? - matlab

I have created a graph with data from an Excel file. I need to keep all the xticks but the xticklabels to appear just on every 6 ticks. I have tried doing this:
tickStep=6;
Sheet=2;
filename='MyData.xlsx';
[~,xAxis]=xlsread(filename,Sheet,'A2:A60');
yAxis=xlsread(filename,Sheet,'B2:B60');
plot(1:numel(xAxis),yAxis)
set(gca,'xtick',1:numel(xAxis))
set(gca,'xticklabel',xAxis(1:tickStep:numel(xAxis)))
Unfortunately it does not work as all the xticks are plotted, but the xticklabels appear upon each xtick instead of every 6th like I was trying to achieve. I have spent quite a long time thinking about the solution:-(. I need help. Thanks.
edit: I've included an image to help answer questions below:
In relation to this post, would it also be possible that the xticks appear every 4 xticks instead of keeping all of them, at the same time that the xticklabels are plotted every 6 xticks?, not sure about this.

You still need to enter an empty string (or empty cell) for the ticks that will have no label. You can do this by replacing the last line with these three lines:
xTickLabels = cell(1,numel(xAxis)); % Empty cell array the same length as xAxis
xTickLabels(1:tickStep:numel(xAxis)) = xAxis(1:tickStep:numel(xAxis));
% Fills in only the values you want
set(gca,'XTickLabel',xTickLabels); % Update the tick labels
Edit in response to questions below...
The reason your labels appear to be offset from the ticks is because the bottom of the letters appears to be getting lined up with the tick mark. I'm guessing you want the text justified such that the center of each letter lines up with the tick mark. There's no way to do this with the standard MATLAB axes, but there are some submissions on the MathWorks File Exchange that convert the tick labels to text objects and thus give you more options for customizing the text properties:
XTICKLABEL_ROTATE by Brian Katz
Format Tick Labels by Alexander Hayes
Once you convert the labels to text objects, you can then adjust the rotation or vertical justification by modifying the 'Rotation' and 'VerticalAlignment' properties, respectively.

Related

Bar graph x-axis titles do not work with many bars

I'm using MATLAB and I want a bar plot.
I plotted the bars, and then give titles to the bars.
If I only using 10 bars it works (see pic1).
But often I need more, mostly 32 bars (see pic2).
In this case the titles are not under the bars. I found out that MATLAB don't overlap the titles. The max value is 15 bars.
Does anyone knows how I can get MATLAB to overlap the titles?
Here is my code for plotting the bars:
y = absSlopes(1, 1:size(absSlopes,2));
hb = bar(y);
set(gca,'XTickLabel',titles);
rotateXLabels(gca, 45);
ylabel('Anzahl');
absSlopes is a matrix and I use the complete first line.
You may want to try XTick in addition to XTickLabel. At least for regular plots you can enforce specific spacings through this setting.
You can do this by an additional setup-command:
set(gca,'XTickLabel',titles);
set(gca,'XTick',absSlopes(1,:));
Of course, you can set both properties in the same line, but I like to keep it simple in case you want to deactiviate/comment one or the other setting later on.

how to add additional label on x-axis on the rightmost in matlab figure?

I have the tick label set for the x-axis already and I also have the label set for the x-axis too. I am seeking a way to put an additional x label to the rightmost of the figure next to the x tick labels, is there any way to do so? Thanks
just like the following figure, the x ticks was added automatically and x label was added by the command xlabel. But I want to add one additional label "add'l" (red) in the figure. But I have many plots and/or subplots and the axes might be different, so I need to add that label with problem instead of manually.
I suggest using a uicontrol that will be carefully placed in the relevant area.
uicontrol('style','text',....);
If your UI is resizable, be sure to have the same units, i.e. if the text units are Normalized, then the axes units should be Normalized as well.
Check out this example on the MATPLOTLIB gallery page: http://matplotlib.org/examples/pylab_examples/alignment_test.html
It shows adding all kinds of different text to the axis.

how to always display "labels" on axis X

If I zoom several time graph all labels from axis X disapear (go away) and there are no visible axis X labels so it is not possible to understand the part of graph where am I.
How can I force matlab to always display labels on axis X and to update them automatically while zooming and to display enough digits so "neighboor" labels must be different.
it depends, are you manually setting the tick marks yourself ('XTick' and 'XTickLabel' axis properties)?
Try this simple example
plot(sin(1:10), 'o-')
without changing anything, you can zoom as much as you want, and the tick labels will always be visible
EDIT
The root cause of the problem is the same as the one raised in your other question, datetick function will manually set the tick labels, thus disabling automatic update on zoom/pan.
The good news is there are already submissions on FEX that tries to solve this exact problem with DATETICK
I run into the same problem even on the new version of MATLAB (r2014). MATLAB does not display sufficient x-axis tick labels as you zoom-in. After several experiments I found the following workaround. Following is a plot before implementing the solution. MATLAB displays only three XTick labels on the x-axis even though there is sufficient space for more (there are often even less labels as you zoom in more).
Suspecting that MATLAB thinks that it does not have sufficient space to display more labels, a workaround can be to rotate the labels. To do that, after you issue the plot commands, e.g.
plot(tsX);
hold on;
plot(tsY);
plot(tsZ);
add the following command
set(gca,'XTickLabelRotation',90);
Now MATLAB plots with more labels
I am going to report this as a bug to the MATLAB guys.

Setting different tick lengths

I have a plot in which I'd like many ticks along the x-axis, yet only some of the ticks will have tick labels associated with them. However, I'd like the tick marks for those that have labels to be longer than those that don't.
I know that you can turn the x-axis MinorTicks on, but that won't let you control where those ticks are actually located. For example, if the x-axis signifies days, I want tick marks every 7 days but labels every 28 days.
Moreover, setting the XTickLength only lets you set the length for every tick mark, and not selectively.
What is the best way to control the tick lengths while allowing you to specify exactly where the ticks should be?
As said by yuk in this SO answer on a similar question about tick labels:
Tick labels are not individual objects. They belong to axes and their properties determined by axes.
What you can do is to remove tick labels and replace them with text
objects. In this case you can control the text properties.
I guess the same applies to tick marks, but it is probably trickier.
Another solution could be to overlay your original axis with a second one, as suggested by bdecaf.
The R2014b update of the graphics engine introduced the following very simple solution:
h = gca;
h.Box = 'off';
h.XRuler.TickLength = 50;
h.YRuler.TickLength = 30;

How to change the format of the numbers displayed on an axis in a MATLAB plot?

I actually have 3 questions:
I have a plot with data that is in the thousands and my plot's axis is diplaying the tick marks as .4 .8 1.0 1.2 and a *10^4 in the lower right. This is a little annoying.
Besides dividing my data by 1000 or hardcodig the tick marks is there a way to change to tick marks from .4*10^4 TO 4000?
Seems like this should be trivial but aftter browsing through all of the figure's properties I can't seem to get an where.
And...once I get 4000 to apear instead of .4*10^4 is there a way to rotate the tick mark label so it is not overlapping the other labels.
And..how do you set how many "major" tick marks there are?
Thanks so much!
ME
Try the following:
x=[4000, 8000, 10000, 12000]; % define the x values where you want to have a tick
set(gca,'XTick',x); % Apply the ticks to the current axes
set(gca,'XTickLabel', arrayfun(#(v) sprintf('%d',v), x, 'UniformOutput', false) ); % Define the tick labels based on the user-defined format
Reference: Mathworks
In regards to the label rotation, it seems that Matlab does not support such feature by its own, but someone wrote a script for the label rotation, and you might want to give it a try.
There's a nice function by Yair Altman called ticklabelformat to do that in case you want to still be able to freely play with the axes afterwards
description:
http://undocumentedmatlab.com/blog/setting-axes-tick-labels-format/
and the download link:
http://www.mathworks.com/matlabcentral/fileexchange/36254-ticklabelformat-set-a-dynamic-format-of-axes-tick-labels