Eduardo
Eduardo

Reputation: 7141

Matlab zoom listener in a GUI

I have a gui that consists of a plot and a static text in MATLAB.

I want to have a zoom listener on the plot so that i can update the static text with the magnification. Is there anyway that i can do this?

Upvotes: 2

Views: 1902

Answers (1)

JustinBlaber
JustinBlaber

Reputation: 4650

Script file (or you can do this as a nested function, whatever you feel like):

f = figure(1);
z = zoom(f);
imshow(ones(400));
xlim = get(gca,'XLim');
t = text(150,150,'hello','fontsize',4000/(xlim(2)-xlim(1)));
set(z,'ActionPostCallback',@(obj,event_obj)testcallback(obj,event_obj,t));

Function testcallback.m file:

function testcallback(obj,event_obj,t)
    xlim = get(event_obj.Axes,'XLim');
    set(t,'fontsize',4000/(xlim(2)-xlim(1)));
end

Output:

enter image description here

Also, here's the matlab documentation on the zoom object if you want to change directly how the zoom function works or mess with some other things:

http://www.mathworks.com/help/matlab/ref/zoom.html

EDIT: Lastly, you can implement this as a nested function to pass the text object. Save this as testfunction.m and then run it in the terminal by simply typing testfunction:

function testfunction

    f = figure(1);
    z = zoom(f);
    imshow(ones(400));
    xlim = get(gca,'XLim');
    t = text(150,150,'hello','fontsize',4000/(xlim(2)-xlim(1)));
    set(z,'ActionPostCallback',@testcallback);

    function testcallback(obj,event_obj)
        xlim = get(event_obj.Axes,'XLim');
        set(t,'fontsize',4000/(xlim(2)-xlim(1)));
    end

end

Upvotes: 3

Related Questions