Incrementing value in GUI with push button does not work

1 view (last 30 days)
I am designing a live calibration application. I have a callback in which a live preview of the webcam is presented and a callback of a pushbutton of which I want to track the amount of times the button was pressed (n). This pushbutton is only accessible if the correct information was obtained of the frame of the image.
My n is defined in the opening function as follows:
% --- Executes just before GUI_stage_3 is made visible.
function GUI_stage_3_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.n=0;
disp("start");
handles.camera=webcam(1);
handles.minArea =2; handles.maxArea = 14000;
handles.calib=[];
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
My live camera function is:
% --- Executes on button press in start_calib.
function im=start_calib_Callback(hObject1, eventdata, handles)
while true
im=handles.camera.snapshot;
im=rgb2gray(im);
im=im(1:2:end,1:2:end);
image(im);
[success, stats, cnt, id0, id1, searchPoints] = gridextractor(im, 1, handles.minArea, handles.maxArea);
drawnow;
if success==1
disp('succes')
%varargout{1}=success;
set(handles.take_calib_im,'visible','on')
else
set(handles.take_calib_im,'visible','off')
end
handles.im=im;
guidata(hObject, handles);
end
And my pushbutton function is:
% --- Executes on button press in take_calib_im.
function take_calib_im_Callback(hObject, eventdata, handles)
handles.n=handles.n+1;
disp(handles.n)
guidata(hObject, handles);
The problem is that the value of n seems to reset randomly. For example, my workspace displays '1 2 3 1 1 1 2'.
I just can't figure out why this random resetting of my n value is happening. When I try to build an example with only a pushbutton it just works fine.
  1 Comment
Myrthe B.
Myrthe B. on 20 Sep 2018
Update: it does work now with global variables, but I know that this is not a good practice in Matlab. So, how can this be fixed with handles?

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 15 Nov 2018
Don't know if it's a typo or not, but note your start_calib_Callback function has hObject1 as the first input instead of hObject.
function im=start_calib_Callback(hObject1, eventdata, handles)
Are there any other functions in your gui where handles.n is used?
  2 Comments
Cris LaPierre
Cris LaPierre on 15 Nov 2018
Edited: Cris LaPierre on 15 Nov 2018
Your issue is one of variable scope. GUI functions each have their own workspace. The particular problem here is that start_calib_Callback never stops executing. The conditions set on the while loop mean it never stops running.
While that function is running, you occassionaly call take_calib_im_Callback. It will try to update handles, but alas, start_calib_Callback always ends up overwriting any changes made with its values for handle. This is why a global variable solved your issue.
If you want to see the count increase without a global, remove the code for updating the handles structure at the bottom of start_calib_Callback.
Now the real challenge - there are updates made to handles in both functions that you want to keep. If you look around, you'll see that adding handles = guidata(hObject) to force an update to the handles structure inside a function has potential. However, while this works in debugger mode if I set a breakpoint in start_calib_Callback, it does not when running without breakpoints.
I suspect if you could find a way to pause start_calib_Callback if take_calib_im_Callback is running, then update the handles structure in start_calib_Callback, that might work.
Cris LaPierre
Cris LaPierre on 16 Nov 2018
Edited: Cris LaPierre on 16 Nov 2018
If you have the image acquisition toolbox, you can use the preview function to see the image without having to use camera.snapshot in an infinite while loop. With the loop gone, you can now get around having two functions running simultaneously and avoid the variable scope issue.
See this question for how to set that up.

Sign in to comment.

More Answers (0)

Categories

Find more on Desktop in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!