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.
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.
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
the 'run' button callback
stop button callback
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.