pushbutton
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi,
can you help me?
I have pushbutton and static text. I need: If I press pushbutton, the value in static text should increase with number 1.
Thank you for your help
댓글 수: 0
채택된 답변
Kevin Holst
2012년 2월 14일
Your code:
function pushbutton1_Callback(hObject, eventdata, handles)
% nacitanie prvku
h=findobj(0,'Type','figure','Tag','figure1');
UserData=get(h,'UserData');
prvok=0;
if ((str2double(get(handles.pushbutton1,'string'))) == 1)
for i=1:UserData.pocet_vetiev;
prvok=prvok+1;
end
set(handles.text1, 'String',{prvok});
end
is auto-generated code from GUIDE that will be run every time the button is pushed. That's what the pushbutton callback function does. As such, you don't need to attempt to check if it is pushed, which is what I think you are trying to do here:
if ((str2double(get(handles.pushbutton1,'string'))) == 1)
the solution here is a one liner (two if you count the function identifier):
function pushbutton1_Callback(hObject, eventdata, handles)
set(h.text1,'string',num2str(str2double(get(h.text1,'string'))+1))
That's what Sean meant by use the engine part of his code.
추가 답변 (1개)
Sean de Wolski
2012년 2월 14일
figure;
ht = uicontrol('units','norm','position',[0.5 0.5 0.1 0.1],'style','text','string','0');
hp = uicontrol('style','push','string','Increment','callback',{@(src,evt,H)set(H,'string',num2str(str2double(get(H,'string'))+1)),ht});
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!