MATLAB: Key press counter not updating

keypress_counter

I wish to count the total number of times a button is pressed.Actually was working on coin recognition system and when a button is pressed I want to calculate its key Press numbers, if button press >1 so that I may able to flush the screen (clear the screen) to load the image again for other operation . I tried to do so but counter is not updated . best regards.

Best Answer

function counter_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.counter = 0; % Store the counter persistently in the handles struct
guidata(hObject, handles);
end
% Get default command line output from handles structure
varargout{1} = handles.output;
% No, not here: counter =0;
end
function pushbutton1_Callback(hObject, ~, handles)
% No, do not reset the counter at each button press: counter=0;
handles = guidata(hObject);
% This is nonsense:
% if(counter>1)
% disp(lallaala);
% end
handles.counter = handles.counter + 1; % Access the variable inside the handles struct
guidata(hObject, handles);
% sum(length(counter)); ???
disp(handles.counter)
end
By the way: sum(length(counter)), counter is a scalar, then its length is 1 in every case. The sum of the length is 1 also.