Fixing the x ticks for a plot

조회 수: 3 (최근 30일)
Raza
Raza 2019년 4월 4일
댓글: Agnish Dutta 2019년 4월 12일
I have a variable named 'a' of the size 20x40 double and I am trying to plot it. At first I ploted it, without specifying the tick values and Matlab is generating the auto ticks for me. Now, I want to take those ticks and multiply it with a constant and update the previous ticks with the new ones.
I approached it using two ways but both the codes have some flaw to them.
1) In this code I am trying to multiply a constant with the old ticks and then updating it with the new ticks but the xtick values don't make sense. They appear right in the workspace but they dont show up correctly on the plot.
xticks = get(gca,'xtick'); % reads old x ticks
constant = 0.25;
new_Xticklabels = arrayfun(@(x) sprintf('%.0f', constant * x), xticks, 'un', 0);
set(gca,'xticklabel',new_Xticklabels);
yticks = get(gca,'ytick'); %reads old y ticks
new_Yticklabel=arrayfun(@(y) sprintf('%.0f', constant * y), yticks, 'un', 0);
set(gca,'yticklabel',new_Yticklabel);
2)In the second code I tried to specify the x and the y ticks and used the labels to show the value but in doing that the graph shows up with empty spots.
xticks([12 20 32 40]);
xticklabels({'3','5','8','10'});
yticks([8 16 24 32 40 48 56 64]);
yticklabels({'2','4','6','8','10','12','14','16'});
Any help would be appreciated.
Thank you.
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 4월 4일
Both versions appear to work for me.
Agnish Dutta
Agnish Dutta 2019년 4월 12일
The first version seems to be working fine.
The second one may be giving problems because the of values you are specifying as input to the "xticks" function may be out of the range.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by