stop timer

I've tried it many times but the result of errors, can you help me give advice my code
function togglebutton1_Callback(hObject, eventdata, handles)
t=timer('TimerFcn',(@axes1.handles),'BusyMode','queue','executionmode','singleShot','period',0.5);
button_state = get(hObject,'Value')
handles.togglebutton1=1
if button_state == get(hObject,'Max')
while handles.togglebutton1==1
start(t)
camorbit(1,0,'data',[0 0 1])
drawnow
end
elseif button_state == get(hObject,'Min')
stop (t)
end
The above code can run but can not be stopped if the return is pressed the button and remove the error message such as this
??? Error while evaluating TimerFcn for timer 'timer-5' Undefined function or method 'desain.handles' for input arguments of type 'timer'.
??? Error while evaluating TimerFcn for timer 'timer-5' Undefined function or method 'desain.handles' for input arguments of type 'timer'.
please help thanks

 Respuesta aceptada

Paulo Silva
Paulo Silva el 4 de Jun. de 2011

0 votos

Your timer function @axes1.handles is completly invalid, it's no function.
Also the way you start and stop the timer isn't correct, should give you too many errors.
Here's a GUI that I made with GUIDE, the fig file is just one axes and one pushbutton, the m file code is:
function varargout = rottest(varargin)
% ROTTEST M-file for rottest.fig
% ROTTEST, by itself, creates a new ROTTEST or raises the existing
% singleton*.
%
% H = ROTTEST returns the handle to a new ROTTEST or the handle to
% the existing singleton*.
%
% ROTTEST('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ROTTEST.M with the given input arguments.
%
% ROTTEST('Property','Value',...) creates a new ROTTEST or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before rottest_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to rottest_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help rottest
% Last Modified by GUIDE v2.5 04-Jun-2011 18:27:20
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @rottest_OpeningFcn, ...
'gui_OutputFcn', @rottest_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before rottest is made visible.
function rottest_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to rottest (see VARARGIN)
% Choose default command line output for rottest
handles.output = hObject;
spy %just to put something in the axes so we can see it rotating
MyTimer=timer('TimerFcn',@RotImgFcn,'BusyMode','queue','executionmode','fixedspacing','period',0.5);
%tag the timer and axes to have easy access to their handles
set(MyTimer,'tag','MyTimer')
set(handles.axes1,'tag','MyAxes')
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes rottest wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = rottest_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
MyTimer=timerfind('tag','MyTimer');
MyTimerStatus=get(MyTimer,'Running');
if strcmp(MyTimerStatus,'off')
start(MyTimer)
else
stop(MyTimer)
end
function RotImgFcn(a,b)
%this function does the rotation
camorbit(findall(0,'tag','MyAxes'),1,0,'data',[0 0 1]);
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% when the user closes the GUI it stops the timer if it is running
% and deletes the timer from memory
MyTimer=timerfind('tag','MyTimer');
MyTimerStatus=get(MyTimer,'Running');
if strcmp(MyTimerStatus,'on')
stop(MyTimer)
end
delete(timerfind('tag','MyTimer')); %delete our timer
%delete(timerfindall) %delete all timer objects
% Hint: delete(hObject) closes the figure
delete(hObject);

15 comentarios

pink
pink el 4 de Jun. de 2011
how to make a start and stop function?
Can help me fix the code
please help
Paulo Silva
Paulo Silva el 4 de Jun. de 2011
there are lots of ways to do it and I have no clue about what you want to do, please explain what you are trying to do
pink
pink el 4 de Jun. de 2011
thanks in advance
I want to rotate objects in axes1,
Initially code like this
function button1_callback_Callback(hObject, eventdata, handles)
for i=1:360
camorbit(1,0,'data',[0 0 1])
drawnow
set(gca,'visible','on')
then I want to change this by using togglebutton and replace the loop by using a timer, so if togglebutton pressed the timer will run and the object will rotate, if togglebutton pressed again then the timer will stop and the object will stop
Paulo Silva
Paulo Silva el 4 de Jun. de 2011
I edited my answer and insert the code, that code won't stop the timer after 360 executions of the timer function, if you want it to stop after X executions, do this in the OpeningFcn:
global ImgAng
ImgAng=0;
And change the timer function code to this:
function RotImgFcn(a,b)
global ImgAng
ImgAng=ImgAng+1
camorbit(findall(0,'tag','MyAxes'),1,0,'data',[0 0 1]);
if ImgAng==360
MyTimer=timerfind('tag','MyTimer');
stop(MyTimer)
ImgAng==0;
end
pink
pink el 5 de Jun. de 2011
I want to like the answer to your first,
maybe you can see my code below, if I bring up the object pressing button2 (surf) on the axes when I press the button1, the object can not be rotated and an error occurs
??? Error while evaluating TimerFcn for timer 'timer-1'
Attempted to access b(3); index out of bounds because numel(b)=0.
??? Error while evaluating TimerFcn for timer 'timer-2'
Attempted to access b(3); index out of bounds because numel(b)=0.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
MyTimer=timerfind('tag','MyTimer');
MyTimerStatus=get(MyTimer,'Running');
if strcmp(MyTimerStatus,'off')
start(MyTimer)
else
stop(MyTimer)
end
function RotImgFcn(a,b)
%this function does the rotation
camorbit(findall(0,'tag','MyAxes'),1,0,'data',[0 0 1]);
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% when the user closes the GUI it stops the timer if it is running
% and deletes the timer from memory
MyTimer=timerfind('tag','MyTimer');
MyTimerStatus=get(MyTimer,'Running');
if strcmp(MyTimerStatus,'on')
stop(MyTimer)
end
delete(timerfind('tag','MyTimer')); %delete our timer
%delete(timerfindall) %delete all timer objects
% Hint: delete(hObject) closes the figure
delete(hObject);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
surf(peaks)
axis vis3d
pink
pink el 5 de Jun. de 2011
I want to like the answer to your first,
maybe you can see my code below, if I bring up the object pressing button2 (surf) on the axes when I press the button1, the object can not be rotated and an error occurs
??? Error while evaluating TimerFcn for timer 'timer-1'
Attempted to access b(3); index out of bounds because numel(b)=0.
??? Error while evaluating TimerFcn for timer 'timer-2'
Attempted to access b(3); index out of bounds because numel(b)=0.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
MyTimer=timerfind('tag','MyTimer');
MyTimerStatus=get(MyTimer,'Running');
if strcmp(MyTimerStatus,'off')
start(MyTimer)
else
stop(MyTimer)
end
function RotImgFcn(a,b)
%this function does the rotation
camorbit(findall(0,'tag','MyAxes'),1,0,'data',[0 0 1]);
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% when the user closes the GUI it stops the timer if it is running
% and deletes the timer from memory
MyTimer=timerfind('tag','MyTimer');
MyTimerStatus=get(MyTimer,'Running');
if strcmp(MyTimerStatus,'on')
stop(MyTimer)
end
delete(timerfind('tag','MyTimer')); %delete our timer
%delete(timerfindall) %delete all timer objects
% Hint: delete(hObject) closes the figure
delete(hObject);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
surf(peaks)
axis vis3d
Paulo Silva
Paulo Silva el 5 de Jun. de 2011
Make sure you have the openingfcn that I put in my answer, the function the does the rotation isn't working, probably because it doesn't find any tagged axes.
pink
pink el 5 de Jun. de 2011
hi paulo,
thank you very much for your help
I spent time for 1 week to find a solution, now everything is working properly
pink
pink el 13 de Jun. de 2011
hi silva
I have a problem with the timer. I try to add 2 pushbutton from the example you give above, when I use worked well initially but gradually became hangs, is there any mistake in my code?
function pushbutton3_Callback(hObject, eventdata, handles)
MyTimer=timer('TimerFcn',@RotImgFcn,'BusyMode','queue','executionmode','fixedspacing','period',0.5);
%tag the timer and axes to have easy access to their handles
set(MyTimer,'tag','MyTimer')
set(handles.axes1,'tag','MyAxes')
view ([90 0])
function pushbutton4_Callback(hObject, eventdata, handles)
MyTimer=timer('TimerFcn',@RotImgFcn,'BusyMode','queue','executionmode','fixedspacing','period',0.5);
%tag the timer and axes to have easy access to their handles
set(MyTimer,'tag','MyTimer')
set(handles.axes1,'tag','MyAxes')
view ([90 0])
Paulo Silva
Paulo Silva el 13 de Jun. de 2011
I see your problem, you create timers in the button callback instead of having just one timer, do this function timerfindall and see if I'm correct or not.
pink
pink el 13 de Jun. de 2011
Can you help me on how to use these timerfindall function in my code above
thanks
Paulo Silva
Paulo Silva el 13 de Jun. de 2011
put it on the button callbacks and see the mess you made, the example I provided doesn't do that mess, please se my code and compare to yours. It hangs due to too many timer objects.
pink
pink el 13 de Jun. de 2011
in your code, when the file (rottest) is run then pushbutton1 pressed the axes will rotate.
now I want to change it like this
1. when the file (rottest) run and then I press pushbutton2, after that I press pusbutton 1 then the axes will rotate.
2. when the file (rottest) run and then I press pushbutton2, after that I press pusbutton 1 then the axes will rotate, then I press again pushbutton3 in the axes are still spinning
3. when the file (rottest) run and then I press pushbutton2, after that I press pusbutton 3 (handle view) it looks axes will be changed, then I press button1 it will spin axes.
I had several times tried to move the timer to pushbutton2, pushbutton3 and pushbutton4 but happen like this
??? Error while evaluating TimerFcn for timer 'timer-9'
Attempted to access b(3); index out of bounds because numel(b)=0.
Paulo Silva
Paulo Silva el 13 de Jun. de 2011
You still have too many timer objects, you just need to create one timer object when you start the GUI and use the buttons callback to stop or start the timer, also never forget to delete the timer when closing the GUI.
Use this to stop all timers stop(timerfindall) and to delete them
delete(timerfindall)
pink
pink el 13 de Jun. de 2011
I've changed it.
whether this timer function can be moved on plotbutton?
I want to handle plotbuton first, and then I handle pushbutton1 (rotatebutton)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Exploration en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 4 de Jun. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by