Azzera filtri
Azzera filtri

[GUI] Update variable in while loop

1 visualizzazione (ultimi 30 giorni)
Przemyslaw Gontar
Przemyslaw Gontar il 11 Ott 2018
Modificato: Stephen23 il 11 Ott 2018
Hello, I have problem with updating variable (handles.Background) in while loop when I press pushbutton. While loop:
function CameraPreviewButton_Callback(hObject, eventdata, handles)
% hObject handle to CameraPreviewButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
while get(hObject,'Value')
img = im2double(handles.GigeCam.snapshot);
data = (img(1,:) + img(2,:))/2;
data = data - handles.Background;
plot(handles.axes1,handles.CamVect,data);
pause(0.1);
drawnow;
end
guidata(hObject, handles);
Callback function:
function SetBackButton_Callback(hObject, eventdata, handles)
% hObject handle to SetBackButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
img = im2double(handles.GigeCam.snapshot);
handles.Background = (img(1,:) + img(2,:))/2;
guidata(hObject, handles);
It only works if I push tooggle button to stop while loop and then push again to start loop.
  1 Commento
OCDER
OCDER il 11 Ott 2018
I'm not quite understanding the setup here. You have 2 callbacks: CameraPreviewButton_Callback and SetBackButton_Callback.
When you push the CameraPreviewButton, there is NO call to the SetBackButton or handles.Background. So when exactly should handles.Background update within the while loop of CameraPreviewButton?

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 11 Ott 2018
Modificato: Stephen23 il 11 Ott 2018
If you want any values of handles to change inside that loop because of something that you did in another callback then you will have to explicitly obtain handles again on each loop iteration:
while ...
handles = guidata(hObject);
...
end
One copy of handles does is not shared between all callbacks: each time you change it within a callback it will create a copy local to that callback. It is only when you store that copy (using guidata) that it will be stored in the parent figure... and only then can you get the updated handles structure (either by triggering a callback, or calling handles=guidata(...)).
Or you could avoid this entire mess by writing your own GUI and using nested functions.

Più risposte (0)

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by