Filter löschen
Filter löschen

[GUI] Update variable in while loop

1 Ansicht (letzte 30 Tage)
Przemyslaw Gontar
Przemyslaw Gontar am 11 Okt. 2018
Bearbeitet: Stephen23 am 11 Okt. 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 Kommentar
OCDER
OCDER am 11 Okt. 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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 11 Okt. 2018
Bearbeitet: Stephen23 am 11 Okt. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by