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

 Accepted Answer

Kevin Holst
Kevin Holst on 14 Feb 2012
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.

5 Comments

john
john on 14 Feb 2012
Thank you for explanation Kevin.
I have created this code, witch works good for me:
if (UserData.prvok < UserData.pocet_vetiev)
UserData.prvok=UserData.prvok+1
set(handles.prvok,'String',{UserData.prvok});
elseif (UserData.prvok > UserData.pocet_vetiev || UserData.prvok == UserData.pocet_vetiev )
set(handles.prvok, 'String','Zadane sú všetky prvky');
end
Result of set(h.text1,'string',num2str(str2double(get(h.text1,'string'))+1)) is NaN....I don't know why?....
Kevin Holst
Kevin Holst on 14 Feb 2012
Is the static text1 intialized with a number in it, or is it blank?
Kevin Holst
Kevin Holst on 14 Feb 2012
My guess is that text1 is starting out blank. If that's the case, then you'll need to either initialize it with a zero when your figure is loaded, or check in your pushbutton callback to see if it's blank and increment it to 1 if it is.
function pushbutton1_Callback(hObject, eventdata, handles)
if strcmp(get(h.text1,'string'),'')
set(h.text1,'string','1')
else
set(h.text1,'string',num2str(str2double(get(h.text1,'string'))+1))
end
john
john on 14 Feb 2012
if it is blank, result is NaN
if there is any initial value in text, result is OK.
So, in my program.....how can I set to 1 for value handles.prvok for example with button pushbutton2:
function pushbutton4_Callback(hObject, eventdata, handles)
h=findobj(0,'Type','figure','Tag','figure1');
UserData=get(h,'UserData');
if (UserData.prvok < UserData.pocet_vetiev)
UserData.prvok=UserData.prvok+1;
set(handles.prvok,'String',{UserData.prvok});
elseif (UserData.prvok > UserData.pocet_vetiev || UserData.prvok == UserData.pocet_vetiev )
set(handles.prvok, 'String','enough');
end
function pushbutton2_Callback(hObject, eventdata, handles)
UserData.prvok=1;
set(handles.prvok,'String',{UserData.prvok});
john
john on 14 Feb 2012
point 1. If I press button4, value increasing...this is OK.
point 2.If I press button2, value is set to 1....this is OK.
point 3. But, if I press in next step button 4, result is not 2, but result is value from point 1 + 1....
I don't understand

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 14 Feb 2012
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});

4 Comments

john
john on 14 Feb 2012
Hi, thank you.
but I have function pushbutton1_Callback(hObject, eventdata, handles) and text1.
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
I don't need button 'Increment', please make it easier.
I want only press button "pushbutton1" and increase value to value for example: 1,1 .....or 2,2...or 3,3......but always increase with 1.
Sean de Wolski
Sean de Wolski on 14 Feb 2012
Okay, so take the engine part of my code and translate it to yours. What I have above does _exactly_ what you asked for.
john
john on 14 Feb 2012
Sorry,
how Can I increase value without button "Increase", but with button "pushbutton1".
Sorry I'm not good in this.
Please help me
john
john on 14 Feb 2012
here is my 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

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Asked:

on 14 Feb 2012

Edited:

a
a
on 12 Oct 2013

Community Treasure Hunt

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

Start Hunting!