user001
user001

Reputation: 1848

matlab zoom callback error associated with plotyy function

I tried to set up a zoom post-callback function to modify a figure in a specific manner upon zooming. This invocation prompted an error that was ultimately traced to the plotyy function.

As a simple example, consider the following block of code which plots two bar elements on a single set of axes and defines a zoom object with the ActionPostCallback behavior set. This block of code executes without any problem and supports the zoom-invoked function detailed in the question a bit further below.

h=figure;
z=zoom(h);
set(z,'ActionPostCallback',@post_callback);
plt1=bar(1:6,1:6);
hold on;
plt2=bar(4:6,1:3);
set(plt2,'FaceColor','r');

Making a simple change, that is, exchanging the bar plotting commands for a plotyy command that assigns each plot object to a separate set of axes, results in non-functioning code that triggers a generally unhelpful error message detailed below. A sample of code that recapitulates the aberrant and undesired behavior is as follows:

h=figure;
z=zoom(h);
set(z,'ActionPostCallback',@post_callback);
[ax,plt1,plt2]=plotyy(1:6,1:6,4:6,1:3,@bar,@bar)
hold on;
set(plt2,'FaceColor','r');

Note that the function post_callback, which is common to both code samples, is given by:

function post_callback(obj,evd)
newLim = get(evd.Axes,'XLim');
msgbox(sprintf('The new X-Limits are [%.2f %.2f].',newLim));

The error message displayed when a zoom operation is performed after running the second but not the first block of code is entirely uninformative:

Warning: An error occurred during the mode callback. 
> In uitools.uimode.fireActionPostCallback at 14
  In zoom>local2DButtonUpFcn at 1332
  In hgfeval at 63
  In uitools.uimode.modeWindowButtonUpFcn at 46
  In uitools.uimode.setCallbackFcn>localModeWindowButtonUpFcn at 58

Studying the relevant lines in each of the files indicated was not helpfu to me, but I post them here in case others are able to discern meaning from them:

uitools.uimode.fireActionPostCallback [lines 9-16]
    try
    if ~isempty(hThis.ActionPostCallback)
        hgfeval(hThis.ActionPostCallback,hFig,evd);
    end
    catch
        warning('MATLAB:uitools:uimode:callbackerror',...
            'An error occurred during the mode callback.');
    end

zoom>local2DButtonUpFcn [lines 1330-1332]
    localDoZoom2D(currentAxes,currentXLim,currentYLim,newXLim,newYLim);
    % Fire mode post callback function:
    hMode.fireActionPostCallback(localConstructEvd(currentAxes));

hgfeval [lines 62-66]
    if cellFunction
        feval(fcn{1},varargin{:},fcn{2:end});
    else
        evalin('base', fcn);
    end

uitools.uimode.modeWindowButtonUpFcn [lines 45-46]
    % Execute the specified callback function
    hgfeval(newButtonUpFcn,hFig,evd);

uitools.uimode.setCallbackFcn>localModeWindowButtonUpFcn [lines 56-58]
    function localModeWindowButtonUpFcn(hFig,evd,hThis,newButtonUpFcn)

    hThis.modeWindowButtonUpFcn(hFig,evd,hThis,newButtonUpFcn);

I would be grateful if anyone could shed any light on the nascence of this error and means by which it can be avoided. Are plotyy and `zoom>ActionPostCallback' fundamentally incompatible?

Upvotes: 0

Views: 348

Answers (1)

patrik
patrik

Reputation: 4558

Ok I have found the error. There was a problem since the final line where the error occured was not in the stack trace. However, the problem is not caused by zoom or plotyy. This can be seen due to the fact that a zoom actually is performed. The error is thrown when you tries to run the postCallbackFcn. The problem is that `sprintf does not accept cell input. A workaround to that can be to have something like this.

function mypostcallback(obj,evd)
newLim = get(evd.Axes,'XLim')'; % Transpose
newLim = horzcat(newLim{:}); % cell input is not allowed for sprintf
msgbox(sprintf('The new X-Limits are [%.2f %.2f] and [%.2f %.2f].',newLim));

Sorry for taking so long time and hope this helps.

Upvotes: 1

Related Questions