No ticks being displayed when using XTickLabel

37 views (last 30 days)
Dear all, The idea was to add ticks label to the x axis with a predefined label. The vector label was a mix of empty cell (i.e. []_ and char (e.g., BL 1, BL 2, SS2 2, SS2 3). However, when plot, there was no tick being displayed. I really appreciate if someone can point what mistake that I have overlooked. The complete code and mat file is attached together with thread to make the discussion easier.
Thanks in advance for any suggestion
load('prob_xtickLable.mat');
LimitRange=14401:27301;
Xaxis_time= [273.5 275.5 277.5 279.5 281.5 283.5 303.5 305.5 307.5 315.5 ...
317.5 319.5 327.5 329.5 331.5 339.5 341.5 343.5 351.5 353.5 ...
355.5 363.5 365.5 367.5 375.5 377.5 379.5 387.5 389.5 391.5];
XLabel=XLabel;
Vector_res1=NaN(length(time_hour),1); % Prelocate with NaN
Vector_res2=NaN(length(time_hour),1); % Prelocate with NaN
Vector_label_Xaxis=num2cell(NaN(length(time_hour),1));
Vector_res1(ismember(time_hour,Xaxis_time))=mean_kss;
Vector_res2(ismember(time_hour,Xaxis_time))=SD_kss;
%%Replace NaN with [].while other cell contained the appropriate label. The use
% of [] to ensure matlab ignore the no label row.
Vector_label_Xaxis = repmat({[]},[(length(time_hour)) 1]);
Vector_label_Xaxis(ismember(time_hour,Xaxis_time))=XLabel;
yyaxis right
errorbar(time_hour(:), Vector_res1(:), Vector_res2(:), '--x','MarkerSize',15)
hold on
yyaxis left
plot(time_hour(:),SleepDrive)
%%Problem: No tick being displayed
xticklabels(Vector_label_Xaxis)
  1 Comment
Jan
Jan on 6 Oct 2017
Edited: Jan on 6 Oct 2017
The line "XLabel=XLabel;" is confusing only.

Sign in to comment.

Accepted Answer

Jan
Jan on 6 Oct 2017
Can you post a minimal example, which reproduces the problem? E.g. this works fine:
plot(1:10, rand(1, 10))
set(gca, 'XTickLabel', {[], '2', 3,4,5,6,7,8,9, 'ten'})
But therefore it is required, that the axes has 10 ticks also. Try:
set(gca, 'Xtick', 1:numel(Vector_label_Xaxis), ...
'XTickLabel', Vector_label_Xaxis);
  2 Comments
balandong
balandong on 6 Oct 2017
Hi Jan, Thanks for the quick and valuable suggestion. Your approach work like a charm and more simple.
I will take note that, for future question, I will make a more simple example that can reproduce the problem
Really appreciate it!
raym
raym on 27 Feb 2021
Hi, Jan's answer saved my life. I was in deep trouble for plot with thousands of xtick, but the label was not shown.Jan's answer happendly solved my problem.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!