hide a single XTickLabel entry

Does anyone have a suggestion for hiding a single entry or creating a blank entry for a string used for XTickLabel?
Let's say I have 5 items plotted with random labels [labels = (2, 4, 5, 16, 29);] but I don't want to have the last item (5) plot on the edge of the plot space. I set the X axis to 0:6 to allow for some white space at both ends of the plot. Then using set(gca, 'XTickLabel',labels), I can label the items as listed. However, because the X axis is now 6 instead of 5, the number 2 shows up (it repeats the string) where there is no data. I tried adding a blank or a NaN to the end of [labels] but neither work as the blank shortens the vector back to 5 and NaN is interpreted as a label. Should I be converting labels to a string of characters and if so, can I add a blank one at the end?
Thanks.
snippet of code:
labels = [2, 4, 5, 16, 29];
y = [12 24 5 35 7];
plot(y)
axis([0 length(y)+1 0 40])
set(gca, 'XTick',1:1:length(y)+1,'XTickLabel',labels)

 Respuesta aceptada

Star Strider
Star Strider el 10 de Ag. de 2014
Convert labels to a cell string, and put an empty value in the last space:
labels = {2, 4, 5, 16, 29, []};
When I ran your code with this labels array, the trailing ‘2’ didn’t plot, as I believe you want.

4 comentarios

Doug G
Doug G el 10 de Ag. de 2014
That worked great. Thanks!
I also tried this for the set command: set(gca, 'XTick',1:1:length(y),'XTickLabel',labels)
Both worked so good to see multiple ways of doing this.
Cheers.
Star Strider
Star Strider el 10 de Ag. de 2014
Editada: Star Strider el 10 de Ag. de 2014
My pleasure!
I suggested that one because it will work if you want to skip over any entry in a label list.
Image Analyst
Image Analyst el 10 de Ag. de 2014
Sounds like Doug should officially "Accept" this answer to give Star credit.
Rohit Deshmukh
Rohit Deshmukh el 28 de Feb. de 2020
I have the same question just that the XLable entry to be hidden changes everytime.
How to make the element in matrix disappear?
If I try '[ ]' it deletes the row completely, i don't to delete it, i just want to replace it with blank element
Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 10 de Ag. de 2014

Comentada:

el 28 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by