Borrar filtros
Borrar filtros

[GUI] Update variable in while loop

1 visualización (últimos 30 días)
Przemyslaw Gontar
Przemyslaw Gontar el 11 de Oct. de 2018
Editada: Stephen23 el 11 de Oct. de 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 comentario
OCDER
OCDER el 11 de Oct. de 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?

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 11 de Oct. de 2018
Editada: Stephen23 el 11 de Oct. de 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.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by