Key press counter not updating

조회 수: 1 (최근 30일)
Ayesha Abbasi
Ayesha Abbasi 2018년 7월 17일
댓글: Ayesha Abbasi 2018년 7월 17일
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.

채택된 답변

Jan
Jan 2018년 7월 17일
편집: Jan 2018년 7월 17일
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.
  댓글 수: 1
Ayesha Abbasi
Ayesha Abbasi 2018년 7월 17일
Thank you sir it works... :)

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

추가 답변 (1개)

Dennis
Dennis 2018년 7월 17일
Do you want to press a button or a key on your keyboard? It is both easy to achieve, but not clear to me which one you want. Your main problem is that you need to store counter, else it will indeed not get updated.
function pushbutton1_Callback(hObject, ~, handles)
counter=0;
handles=guidata(hObject);
try
counter=handles.counter;
end
if(counter>1)
disp('lallaala');
end
counter = counter+1;
handles.counter=counter;
guidata(hObject,handles);
sum(length(counter)); %counter is a scalar, this will always return 1
disp(counter)
end

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by