开发者

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

开发者 https://www.devze.com 2023-04-06 15:02 出处:网络
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,

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_e开发者_开发知识库nd
            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.


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);
.....


If you have the parallel computing toolbox, then you can create a Task.

Otherwise, I don't think it's possible without adding the control flow you suggest since there's no other way of creating a processing thread and stopping it from your UI.


what you basically want is to kill the process_data once the user presses the 'stop' button. Unfortunately, this is not possible because there are no real threads in matlab. Therefore, your only solution is the one you've implemented yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消