Search code examples
user-interfacematlabcallbackcontrol-flow

How can you Interupt a function from a GUI callback without adding an interupt-check to the function? (MATLAB)


So, I've a GUI that basically allows the user to iteratively process data. Thus, there is a start/stop button and a display that shows the current state of the data. When you click the start button, the callback function calls the data processing function shown below:

function result = process_data(data)
    result = 0;
    for big_loop=big_start:big_end
        for small_loop=small_start:small_end
            result = result+data; %in reality just some fancier matrix ops
        end
    end

My problem is how to implement the stop button's callback so that it returns from process_data after the current iteration of the small loop.

Right now I do this by modifying the process_data code to be as follows:

    function result = process_data_new(data)
        result = 0;
        for big_loop=big_start:big_end
            for small_loop=small_start:small_end
                result = result+data; %in reality just some fancier matrix ops
%new code start -----------------
                if interrupt_flag
                    return;
                end
%new code end -------------------- 
            end
        end

However, I really want the interruption to be handled from the GUI's end so my code isn't littered with interrupt checks (My real code would involve this kind of thing very often, and the user would sometimes need to change the process_data function).

Is this possible? I imagine it would involve making all of the looping variables 'observable' properties, and then waiting for small_loop to change, but I can't figure out details of how to go about implementing the callback.


Solution

  • may be this helps:

    have a button 'stop', in its callback, set a flag to true.

    in the smaller loop (i.e. the other callback which busy in a loop), it will check this flag at the top (or bottom) of its (small) loop.

    If the flag is set true, it makes it false right away and terminate the overall loop and returns. (add a check in your loop to check if this flag is set)

    So, the most the user has to wait after hitting STOP is for one smaller iteration to complete if the hit happened just after the last check.

    callbacks interrupt each others, so the above works.

    The flag is set in userData buffer and read from there by the other callback each time.

    This snippets code from one example I have

    the small loop example

     function status= output(t,x,~,data)
             %called by ode45 after each step. Plot the current
             %pendulum position for simulation
    
             userData = get(data.handles.figure1, 'UserData');
             if userData.stop
                 status=true;
                 g_status =true;
             else
                 status = false;
                 .... do real work
             end
         end
    

    the 'run' button callback

    % --- Executes on button press in run_btn.
    function run_btn_Callback(hObject, eventdata, handles)
    % hObject    handle to run_btn (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    [data,status] = parse_input(handles);
    if not(status)
         return;
    end
    .....
    userData.stop = false;
    set(handles.figure1, 'UserData',userData);
    ...
    

    stop button callback

    % --- Executes on button press in reset_tag.
    function reset_tag_Callback(hObject, eventdata, handles)
    % hObject    handle to reset_tag (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    
    data = get(handles.figure1, 'UserData');
    data.stop = true;
    set(handles.figure1, 'UserData',data);
    .....