MATLAB Answers

pink
0

stop timer

Asked by pink
on 4 Jun 2011
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

  0 Comments

Sign in to comment.

Tags

Products

1 Answer

Answer by Paulo Silva
on 4 Jun 2011
 Accepted Answer

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 Comments

pink
on 13 Jun 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
on 13 Jun 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
on 13 Jun 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)

Sign in to comment.