Search code examples
matlabmatlab-figure

restart matlab window button motion after a temporary disabling


I am designing a Matlab GUI. In this GUI, windowButtonMotion constantly takes input. However, I need to stop it for a little while and ask the user a visual input from another figure. This figure closes after the input and the programme is supposed to work the same way.

I disable the WBM with this code when the new figure pops up:

set(fighandle, 'WindowButtonMotionFcn', '');

The question is, how can I restart the WBM after input is taken.

I do not call the WBM with a function call like:

set(fighandle, 'WindowButtonMotionFcn', @fun);

In the contrary, I write the codes under the windowButtonMotion_Fcn callback in the GUI.


Solution

  • The easiest thing to do is to store the callback before you change it and then you can reset it to the same thing. This will work regardless of what your callback is.

    % Store the original
    originalCallback = get(fighandle, 'WindowButtonMotionFcn');
    
    % Disable the callback
    set(fighandle, 'WindowButtonMotionFcn', '')
    
    % Do stuff
    
    % Now reset it
    set(fighandle, 'WindowButtonMotionFcn', originalCallback)
    

    If these do not happen within the same function, you can store the previous callback within the figure's app data.

    % Get original callback and store within the appdata of the figure
    setappdata(fighandle, 'motioncallback', get(fighandle, 'WindowButtonMotionFcn'));
    
    % Disable the callback
    set(fighandle, 'WindowButtonMotionFcn', '')
    
    % Then from another function re-enable it
    set(fighandle, 'WindowButtonMotionFcn', getappdata(fighandle, 'motioncallback'))
    

    Your other option is to actually open your second figure as a modal figure and then the mouse interaction with the background figure will be ignored. Then you will not have to tamper with the WindowButtonMotionFcn.

    fig2 = figure('WindowStyle', 'modal');