필터 지우기
필터 지우기

[GUI] Update variable in while loop

조회 수: 1 (최근 30일)
Przemyslaw Gontar
Przemyslaw Gontar 2018년 10월 11일
편집: Stephen23 2018년 10월 11일
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
OCDER
OCDER 2018년 10월 11일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 10월 11일
편집: Stephen23 2018년 10월 11일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by