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