[GUI] Update variable in while loop

1 次查看(过去 30 天)
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