octave - using timer in MATLAB to extract the system time -
!i using matlab design analog clock. currently, code displays (or plots rather) clock design hands (hours, mins, secs) , not tick. here code:
function raviclock(h,m,s) drawclockface; %timer begins------- t = timer; t.executionmode = 'fixedspacing'; %use 1 of repeating modes t.period = 1; %fire on 1 second intervals t.timerfcn = @timer_setup; %when fired, call function start(t); set(gcf,'deletefcn',@(~,~)stop(t)); end function timer_setup(varargin) format shortg; timenow = clock; h = timenow(4); m = timenow(5); s = timenow(6); % hour hand hours= h + m/60 + s/3600; hourangle= 90 - hours*(360/12); % compute coordinates pointing end of hour hand , draw [xhour, yhour]= polar2xy(0.6, hourangle); plot([0 xhour], [0 yhour], 'k-','linewidth',7.4) % minute hand mins= m + s/60; minsangle= 90 - mins*(360/60); % compute coordinates pointing end of minute hand , draw [xmins, ymins]= polar2xy(0.75, minsangle); plot([0 xmins], [0 ymins], 'r-','linewidth',4) %second's hand second = s; secangle = 90- second*(360/60); [xsec, ysec]= polar2xy(0.85, secangle); plot([0 xsec], [0 ysec], 'm:','linewidth',2) %end % while ends end %-------------------------------------------------------- function drawclockface %close axis([-1.2 1.2 -1.2 1.2]) axis square equal hold on theta= 0; k= 0:59 [xx,yy]= polar2xy(1.05,theta); plot(xx,yy,'k*') [x,y]= polar2xy(0.9,theta); if ( mod(k,5)==0 ) % hour mark plot(x,y,'<') else % minute mark plot(x,y,'r*') end theta= theta + 360/60; end end %----------------------------------------------------------------- function [x, y] = polar2xy(r,theta) rads= theta*pi/180; x= r*cos(rads); y= r*sin(rads); end
this taking static data of values hour, minute , second arguments when call function. tried using following in while loop didn't much
format shortg c=clock clockdata = fix(c) h = clockdata(4) m = clockdata(5) s = clockdata(6)
and passing h, m , s respective cuntions. want know how can use timer obkjects , callbacks extracting information of [hrs mins secs] can compute respective point co-ordinates in real time clock ticks.
i'd couple of things here.
first, don't need pass h
, m
, s
inputs, if displaying current time. add top of function auto set these variables.
if nargin == 0 [~,~,~,h,m,s] = datevec(now); end
then, pretty easy use time call function periodically. this.
t = timer; t.executionmode = 'fixedspacing'; %use 1 of repeating modes t.period = 1; %fire on 1 second intervals t.timerfcn = @(~,~)raviclock; %when fired, call function (ignoring 2 inputs) start(t); %go!
use docsearch timer
in depth documentation of timer objects. code above should started.
to stop timer, run
stop(t);
to stop timer when window closed, put stop command window deletion callback:
set(gcf,'deletefcn',@(~,~)stop(t)); %note: better explicitly use figure number, rather gcf.
Comments
Post a Comment