How to modify the y-axis tick label and tick values (2016a)

I have three variables day, time and z2 and i plot using imagesc. Below are the data information:
data information:
day 1*1420 double(each number is equal to 6 hours)
time 1*3001 double (this case has -300:300 with interval of 0.2)
z2 1420*3001 double
Later I converted the weeks segments into date by using below code.
fig=figure(ii);
h1=subplot(3,1,1);
imagesc(time,day,z2);
colormap(jet)
YTickStr = char(datetime('10/10/2018', 'InputFormat', 'dd/MM/yy', 'Format', 'dd/MM/yy') + hours(day*6));
set(gca, 'YTick', 1:lengday, 'YTickLabel', YTickStr);
xlim([-100 100]);
If i do not use any interval, ticks label are so difficult to read as below and I cant read any thing
Later, I change the interval with 1:500:lengday and it starts from 1JAN which i exected to start from 10/10/18.
How can I modify the code so that it gives me the interval based on Ytrickstr?
here is my failed attemt which gives me the first 5 values from YTickStr.
yval = ylim(gca);
set(gca,'YTick',linspace(yval(1),yval(2),5),'YTickLabel', YTickStr)
YTickStr files is like this
10/10/18
10/10/18
10/10/18
11/10/18
11/10/18
11/10/18
11/10/18
12/10/18
12/10/18
12/10/18
12/10/18
13/10/18
13/10/18
13/10/18
13/10/18
14/10/18
Thank you very much

 Accepted Answer

Rik
Rik on 23 Jun 2021
Edited: Rik on 23 Jun 2021
The trick is to select part of your YTickStr:
L=round(linspace(1,size(YTickStr,1),5));
% use round to round to integer indices
yval=ylim;yval=linspace(yval(1),yval(2),numel(L));
%this is only approximate
set(gca,'YTick',yval,'YTickLabel', YTickStr(L,:))
%exact yval:
yval=yval(1) + diff(yval)* (L-1)./(numel(L)-1);

6 Comments

AA
AA on 23 Jun 2021
Edited: AA on 23 Jun 2021
Thank you boss, it work but it can only give me first and last ticks. Even there are 5 ticks in the script at different intervals,
How can we increase number of ticks in this case?
I don't understand what you mean. This code should result in 5 ticks. Can you post code that will generate random data for your variable (or upload a mat file with your actual variables)?
I also don't understand why you copied most of my answer and accepted it.
sorry, I didnt mean to reject your answer. I should post in the reply section, please accept my apology .
thanks for comments
problem in your code was that yval has only two values, first one and last one so it will always give two ticks even we change number of ticks. I just modified to get equal numbers for ytick and yticklabel.
No problem, this forum can be less intuitive for newer members.
yval is derived from ylim, which returns only two values, but the second part of the line expands this to the same count as L, which was set to 5 in the line before it.
So yval and L should have the exact same number of elements.
For sure, I checked above script and it does not work that's why shared the modified code. We do not need to use Ylim. BTW, thanks again.

Sign in to comment.

More Answers (1)

imagesc(time,day,z2);
colormap(jet)
YTickStr = char(datetime('10/10/2018', 'InputFormat', 'dd/MM/yy', 'Format', 'dd/MM/yy') + hours(day*6));
yval = ylim(gca);
L=round(linspace(1,size(YTickStr,1),5));
set(gca,'YTick',day(:,L),'YTickLabel', YTickStr(L,:))

Asked:

AA
on 23 Jun 2021

Commented:

AA
on 24 Jun 2021

Community Treasure Hunt

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

Start Hunting!