Get a GUI to update while a loop is running

21 Ansichten (letzte 30 Tage)
MechtEngineer
MechtEngineer am 31 Mär. 2011
Kommentiert: Lance Darragh am 20 Apr. 2018
I have a GUI with a "Run" button that runs a filter on a continuous stream of images. The "Run" button callback function is "interruptible" = "on" and the "BusyAction" is "queue". I then adjust a slider which adjusts the filter that the "Run" loop is using and updates the GuiData handles variable. The problem is that the filter update is not recognised, until I stop the "Run" loop, and repress the "Run" button. Does anyone know how to get a running loop to use the latest version of a variable in the GuiData handles structure?
For example, the Run Function
function run_pushbutton_Callback(hObject, eventdata, handles)
if get(handles.run_pushbutton,'UserData')==1, return; end
set(handles.run_pushbutton,'UserData',1);
while (get(handles.run_pushbutton,'UserData') ~= 0)
% Increment the image number
handles.curImgNum = handles.curImgNum + 1;
guidata(hObject,handles); % Save the data
tempFiltWgt = handles.temporalFiltWeight/100
% Run the filter weight on the images...
% ...
end
Stop Function
function stop_pushbutton_Callback(hObject, eventdata, handles)
disp('STOP pressed.')
set(handles.run_pushbutton,'UserData',0);
Slider Function
function temporal_slider_Callback(hObject, eventdata, handles)
%obtains the slider value from the slider component
handles.temporalFiltWeight = get(handles.temporal_slider,'Value');
% Update handles structure
guidata(hObject, handles);
I have taken out all of the non-essentials to make the code clear. I am constantly updating the GuiData handles structure to try to keep everything up to date, but it won't seem to be updated in a function that has been interrupted. Is there any solution?

Akzeptierte Antwort

Teja Muppirala
Teja Muppirala am 31 Mär. 2011
Instead of having this
%obtains the slider value from the slider component
handles.temporalFiltWeight = get(handles.temporal_slider,'Value');
in the slider callback, you could put this code as the first line inside the while loop in the run_pushbutton callback. Would that work?
  1 Kommentar
MechtEngineer
MechtEngineer am 31 Mär. 2011
Absolutely spot on Teja! It works brilliantly. Before I had to actually stop the loop and then change the value and then restart, now I can change the setting live and view the results! Thanks a lot.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Titus Edelhofer
Titus Edelhofer am 31 Mär. 2011
Try to put a drawnow into the loop to allow the event queue to be processed.
Titus
  3 Kommentare
MechtEngineer
MechtEngineer am 31 Mär. 2011
Could you advise where I put the drawnow? I have tried putting the drawnow at various spots within the "run" loop and it hasn't made any difference.
I put in
drawnow;
and
drawnow; pause(0.1);
Neither worked, and I put them in multiple places within the "run" and "slider" loops. I was a bit surprised when it didn't work.
If you don't know why, don't worry about it, as Teja Muppirala's advice down below works a treat! But I would like to see the performance with drawnow too.
Lance Darragh
Lance Darragh am 20 Apr. 2018
I had a similar issue as in this post. Adding the drawnow command as the second line (the first being the one suggested as the original solution) fixed the problem, both were needed. Neither one by itself fixed the problem. Thank you all for your suggestions. By the way, it doesn't hurt to keep the statement in the slider callback function as well, in case it is needed outside of this while loop.

Melden Sie sich an, um zu kommentieren.


Robert Cumming
Robert Cumming am 31 Mär. 2011
As well as the drawnow command you could try putting a pause (0.1) in - this can help refresh graphics in the queue.
  2 Kommentare
Jan
Jan am 31 Mär. 2011
Some Java updates are performed after PAUSE(0.02) only, e.g. to get the contents of UICONTROLs by GETFRAME. But in the OP's problem 0.1 sec would mean a remarkable slowdown, so I'd prefer DRAWNOW.
Robert Cumming
Robert Cumming am 31 Mär. 2011
To be honest I didn't examine the OP code, only the general problem - and my answer was to complement the drawnow if it didn;t do it (for whatever reason), and do agree that it could cause a slow down if called a lot!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by