How should i add a text in the plot in order this autoscale when i make a zoom in or zoom out?
Both FontUnits
options, normalized
and pixels
do not rescale when zooming in or zooming out.
figure(1);
text(0.5,0.5,'test',...
'FontUnits','Normalized',...
'FontSize',0.25,...
'HorizontalAlignment','center',...
'Color',color...
);
figure(2);
set(gcf,'Position',[935 428 672 504])
text(50,50,'test',...
'FontUnits','pixels',...
'FontSize',100,...
'HorizontalAlignment','center',...
'Color',color...
);
axis([0 100 0 100])
This function, including a further modification on the zoomCallBack function:
function drawtext(p,s,f,color)
% get axes size
ax = axis;
% add some text
ax0=ax(4)-ax(3);
txt = text(p(1),p(2),s,...
'FontSize',f,...
'HorizontalAlignment','center',...
'Color',color);
h = zoom; % get handle to zoom utility
set(h,'ActionPostCallback',@zoomCallBack);
set(h,'Enable','on');
% everytime you zoom in, this function is executed
function zoomCallBack(obj, evd)
% Since i expect to zoom in ax(4)-ax(3) gets smaller, so fontsize
% gets bigger.
ax = axis(evd.Axes); % get axis size
% get all texts in figure
htxt = findobj(gcf,'Type','text');
axi=ax(4)-ax(3);
for i=1:length(htxt)
% change font size accordingly
set(htxt(i),'FontSize',str2num(get(htxt(i),'Tag'))*ax0/axi);
end
end
end
This is a working solution, but, very tricky and fails sometimes. Any better solution is welcome.