MATLAB: How do i stop the Timer Object from running

MATLABmatlab functionreal timestopfcntimertimer functiontimer objecttimer object functiontimer_object

Hey guys, I have this matlab function which is a timer function. It continuously reads numerical data from a text file (which updates every 10ms) and plot this data with time every (plot updates every second). In matlab, i type "timerPlot" to run the timer object function, but i have no idea how to stop it fro running. The only way i can stop it is if i close the plot window (which ends the timer via producing an error). Ive tried using stop(t) but it just says "Undefined function or variable 't'." Heres the code:
function [t] = livePlot(period, filename)
period = 1;
filename = 'C:\Program Files\...' %%Location of text file
close all;
t = timer('StartDelay', 1, 'Period', period, ...
'ExecutionMode', 'fixedRate');
%%timer object callback functions
t.StopFcn = {@stopFigure};
t.TimerFcn = {@updateFigure};
%%initialize timer object user data
d = get(t, 'UserData');
d.data = []; % array for the data to plot
axes('Position', [0 0 1 1], 'Visible', 'off');
d.axes_handle = axes('Position', [.2 .1 .7 .8]);
d.line_handle = plot(NaN,NaN);
d.fid = fopen(filename, 'r');
set(t, 'UserData', d);
start(t);
end
function stopFigure(obj, event)
% close function handle
d = get(obj, 'UserData');
fclose(d.fid);
delete(t)
disp('Timer has stopped.');
end
function updateFigure(obj, event)
d = get(obj, 'UserData');
% read new data from file
tmp = readFile(obj);
% append to array in user data
d.data = [d.data transpose(tmp)];
% update the plot
set(gcf, 'CurrentAxes', d.axes_handle);
set(d.line_handle, 'XData', 1:length(d.data), 'YData', d.data);
% store the timer object user-data
set(obj, 'UserData', d);
end
function [tmp] = readFile(obj)
% read binary data. file-ID is in the timer user-data
d = get(obj, 'UserData');
tmp = fscanf(d.fid, '%f', inf);
fprintf('Current file location : %d \n', ftell(d.fid));
fseek(d.fid, -1, 0); %%Reset the file position indicator back to beginning of file
fseek(d.fid, 1, 0);
set(obj, 'UserData', d);
end
Thanks.

Best Answer

You may find this useful:
%---------------------------------------------------------------------
function StopTimer(handles)
try
fprintf('Entering StopTimer...\n');
listOfTimers = timerfindall % List all timers, just for info.
% Get handle to the one timer that we should have.
if isempty(listOfTimers)
% Exit if there is no timer to turn off.
fprintf('There are no timers to turn off. Leaving StopTimer().\n');
return;
end
handleToTimer = getappdata(handles.figMainWindow, 'timerObj');
% Stop that timer.
stop(handleToTimer);
% Delete all timers from memory.
listOfTimers = timerfindall
if ~isempty(listOfTimers)
delete(listOfTimers(:));
end
fprintf('Left StopTimer and turned off all timers.\n');
catch ME
errorMessage = sprintf('Error in StopTimer().\nThe error reported by MATLAB is:\n\n%s', ME.message);
fprintf('%s\n', errorMessage);
WarnUser(errorMessage);
end
return; % from StopTimer