Search code examples
matlabuser-interfacematlab-figure

Matlab push button to continue


Inside my main function I call a function that need to do some stuff before continue the program and I want to add a push button that allows to continue if the results are correct or start again the function to redo the calculations. I tried in this way adding the first button that allows to continue the program

h1  = figure(1);
% plot stuff...
button = uicontrol('Parent', h1,'Style','pushbutton',...
         'Units','normalized',...
         'Position',[0.4 0.3 0.2 0.1],...
         'String','Display Difference',...
         'Callback',@button_callback);

function button_callback(hObject,eventdata)
    if get(hObject,'Value') == 0
        %do nothing
    else
        return
    end
end

But doesn't work because the program continues also without push it. What am I missing?


Solution

  • You have to explicitly tell Matlab to stop and wait for user's input. You can do that using the uiwait command

    So the code would be (even if I did not test it):

    h1  = figure(1);
    % plot stuff...
    button = uicontrol('Parent', h1,'Style','pushbutton',...
             'Units','normalized',...
             'Position',[0.4 0.3 0.2 0.1],...
             'String','Display Difference',...
             'Callback',@button_callback);
    uiwait(h1);
    
    function button_callback(hObject,eventdata)
        if get(hObject,'Value') == 0
            %do nothing
        else
            uiresume;
            return
        end
    end
    

    In Matlab there are also predefined dialog box that you can use for this purpose.