From: Christian on
hi everybody¡¡¡
I'm new in it. I created a GUI just like a timer, with a pushbutton and a edit text.
When i push the button cmdMain it begin from 1 to 100 and show this value on the edit text.
The pushbutton have two string, first "Empezar (begin)" and After I pushed it, the button show "Parar (stop)"

function cmdMain_Callback(hObject, eventdata, handles)
S.r=handles.txtResultado;
tmr=timer('Period',0.1,'ExecutionMode','fixedSpacing','TaskstoExecute',100,...
'TimerFcn',{@updater,S});
if strcmp(get(hObject,'String'),'Parar')
stop(tmr);
delete(tmr);
set(hObject,'String','Empezar');
else
start(tmr);
set(hObject,'String','Parar');
end
function updater(varargin)
S=varargin{3};
data=get(S.r,'UserData');
data.tiempo=data.tiempo+1;
set(S.r,'UserData',data);
set(S.r,'String',num2str(data.tiempo));

But when I push the button when the string of the cmdMain is "Parar (stop)" the timer function don't stop.
I don't know the reason of it. Please help me¡¡
Sorry for my english....
From: Jan Simon on
Dear Christian,

> function cmdMain_Callback(hObject, eventdata, handles)
> S.r=handles.txtResultado;
> tmr = timer('Period',0.1, 'ExecutionMode','fixedSpacing', 'TaskstoExecute',100,...
> 'TimerFcn',{@updater,S});
> if strcmp(get(hObject,'String'),'Parar')
> stop(tmr);
> delete(tmr);
> set(hObject,'String','Empezar');
> else
> start(tmr);
> set(hObject,'String','Parar');
> end

Each time you call cmdMain_Callback a new timer object is created. If the string of the button is 'Parar', this timer object is stopped and deleted, but not the one, which was created formerly.
Solution: Create the time only once and store the handle in the figure's UserData.

Good luck, Jan
From: Christian on
> Solution: Create the time only once and store the handle in the figure's UserData.
Thanks, It Works¡¡¡¡