The XTickLabel command is not working properly.

I am trying to set the 2D graph axis label using h.XTickLabel = dx*h.XTick. Problem comes when I am stretching the graph.
First I plotted the graph ticks as 0,80,160,240,300, using above command. However when I am stretching the figure, axis numbers changes. Matlab automatically created almost twice of subdivisions and now the axis ticks are 0,80,160,240,300,0,80,160,240.

 채택된 답변

Marc Jakobi
Marc Jakobi 2016년 10월 9일
편집: Marc Jakobi 2016년 10월 9일

0 개 추천

You could try
f = gcf;
f.ResizeFcn = 'h.XTickLabel = dx.*h.XTick;';
This will call
h.XTickLabel = dx.*h.XTick;
every time the figure resizes. However, if you would like to do this to more than one graphics object, you should put it in a separate function and put that function in the ResizeFcn.

댓글 수: 3

Hii Mark. Thanks for reply. I have been trying this approaches but couldn't get through. I couldn't find ResizeFcn property in the figure properties but I found that now it is replaced by new property named SizeChangeFcn (2016a).
nh=100, nv=200;
dh=10; dv=5;
mat=rand(nv,nh);
subplot(2,2,2); imagesc(0:nh-1,0:nv-1,mat)
h=gcf
g=get(gcf,'children');
h.SizeChangedFcn = 'g.XTickLabel=dh*g.XTick;';
However, during stretching it gives error: Undefined function or variable ''dh'.
when I used
SizeChangedFcn = 'g.XTickLabel=10*g.XTick;';
It again produces error undefined variable "g" or glass "g.XTick".
Any help is appreciated.
Marc Jakobi
Marc Jakobi 2016년 10월 10일
편집: Marc Jakobi 2016년 10월 10일
If the variable dh is not in the workspace, you will have to save it. You could save it in the axes's UserData like this:
nh=100; nv=200;
dh=10; dv=5;
mat=rand(nv,nh);
subplot(2,2,2); imagesc(0:nh-1,0:nv-1,mat);
h=gcf;
ax = gca;
ax.UserData = dh;
g=get(gcf,'children');
h.SizeChangedFcn = 'set(gca,''XTickLabel'',get(gca,''UserData'').*get(gca,''XTick''));';
clear
However, for reasons of flexibility, I would recommend you write a separate function, since the above method will only work on a single subplot:
function axTickUpdate()
AX = findobj(gcf,'type','axes');
for i = 1:length(AX)
ax = AX(i);
ax.XTickLabel = ax.UserData.dh.*ax.XTick;
end
end
You can then create the figure like this:
nh=100; nv=200;
dh=10; dv=5;
mat=rand(nv,nh);
subplot(2,2,2); imagesc(0:nh-1,0:nv-1,mat);
h=gcf;
ax = gca;
ax.UserData.dh = dh;
g=get(gcf,'children');
h.SizeChangedFcn = 'axTickUpdate;';
clear
Marc Jakobi
Marc Jakobi 2016년 10월 10일
NOTE: You will have to add dh to the UserData of each axes if you want it to work. Just set it to 1 if you don't want to manipulate the XTicks.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 10월 9일

0 개 추천

Set the xticklabelmode to manual

댓글 수: 1

AM
AM 2016년 10월 10일
편집: AM 2016년 10월 10일
Hii Walter. Thanks for reply. I tried as you suggested but it didn't help.
Instead of it setting XTickMode = 'manual' could do the job.
Only problem with it is the axis limits are not set as desired.
E.g. axis ticks are plotted as : "0,50,100,.....400,450" .
Actually set of ticks should be :"0,50,100,.....400,450, 500".
Last tick label is missing each time I plot.

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

카테고리

질문:

AM
2016년 10월 9일

댓글:

2016년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by