-1

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.

Brethlosze
  • 1,533
  • 1
  • 22
  • 41

1 Answers1

2

text object has property FontUnits. If it is set to Normalized, the text will re-scale with axis.

text(0.4, 0.5, 'test', 'FontUnits', 'Normalized', 'FontSize', 0.2);

Then try to change axis size.

Xiangrui Li
  • 2,386
  • 2
  • 13
  • 17
  • No, the text don't get scaled when i make zoom. It keeps a constant size, and changes with the figure, not with the zooms in the axis. I am seeking exactly the opposite, the text augmented or reduced according the axis zoom... – Brethlosze Jul 11 '17 at 03:55
  • Don't know how you tested it. It works well for me. Here is what documentation says for "Normalized" FontUnit: "Interpret font size as a fraction of the axes plot box height. If you resize the axes, the font size modifies accordingly. For example, if the FontSize is 0.1 in normalized units, then the text is 1/10 of the plot box height.". – Xiangrui Li Jul 11 '17 at 12:53
  • Check the edit for a clearer argument. The text must rescale when i zoom in or zoom out. My attempts failed as you can see. – Brethlosze Jul 24 '17 at 06:30
  • While the text re-scale when we change axis size by *set* command, it won't change as we zoom in or out. The get(ax, 'xlim') shows it is changed after zooming. It is a little strange. I can think of a stupid way to achieve text scaling. Just use default *FontUnits*. You can store current *xlim* and *ylim* in *UserData* of the figure or other object, and in your *zoomCallBack*, you get new *xlim*/*ylim*, and compute the zooming factor, then change the font size accordingly. – Xiangrui Li Jul 24 '17 at 22:06
  • I put that on the suggested code. Not too nice but... works. – Brethlosze Jul 24 '17 at 22:21
  • Sorry I missed that. You can, again, store multiple text handle into *UserData*, and get those handle inside CallBack, so multiple text size can be updated. – Xiangrui Li Jul 24 '17 at 23:57
  • You said it fails sometimes. Any error message when it fails? One possible reason is that the *gcf* in the callback is not ideal, since it returns the top figure handle. If this is the cause, you may try get(ax, 'Parent') to get the figure handle. – Xiangrui Li Jul 28 '17 at 00:58
  • It fails: do not rescale the text if one make a double-click. Which i will not fix it into making dense GUI callbacks. I am looking for a cleaner solution, not to he fixing matlab.... – Brethlosze Jul 28 '17 at 07:33