MATLAB Examples

# MATLAB DATAVIS Contest - CONTEST CLOCK

Author: Rafal Kasztelanic

Creation Date: 08.04.2009

## Contest clock

Below is an animation showing how the results versus the execution times evolved over the contest. The trajectory shows how results change hours by hours during the contest. The distance from center of the clock is proportional to log of results. Getting MATLAB to show this movie required embedding special html code into the file, which should work with most any modern browser.

```load contest_data ```

## Prepare date

```[s,p,leaders] = prepareData; orders = 3; isLeader = [p.isLeader]; mi = min([p(isLeader).rawScore]); ma = max([p(isLeader).rawScore]); y = normalize([p.rawScore],orders,mi,ma); w = y(isLeader); ```

## Prepare results

```ww=log(w); ww = repmat(ww,10,1); ww = reshape(ww,1,numel(ww)); mww = max(ww); ww=115*ww/mww; ```

## Prepare hours

hours

```mint = min([p(isLeader).date]); hour = [p(isLeader).date]-mint; ```

## change hours to angle

```angle = 2*round(hour(end))*2*pi*(hour/round(hour(end))); angle = linspace(min(angle), max(angle), numel(ww)); ```

## Prepare background image

```a=imread('clock.png'); a = imresize(a,0.333); % avi size [sx,sy,sz]=size(a); ```

## Calculate final trajectory points and colors

```xx=sx/2+ww.*cos(-angle+pi); yy=sy/2+ww.*sin(-angle+pi); c=jet(numel(ww)); ```

## Create frames

```kl = 1; anglestop = pi/6; for k=2:numel(ww), a=bitmapplot(xx(k-1:k),yy(k-1:k),a,struct('LineWidth',1.5,'Color',[c(k-1,:) 1])); if angle(k)>anglestop, day = ['Day ' num2str(floor((anglestop-0.01)/4/pi)+1) ',' ... num2str(kl-24*fix(kl./24)) ':00']; b=bitmaptext(day,a,[1 1],struct('FontSize',3,'Color',[0 0 0 1])); b=bitmapplot([sx/2 sx/2+73*cos(-anglestop+pi)],... [sy/2 sy/2+73*sin(-anglestop+pi)],b,... struct('LineWidth',5,'Color',[0 0 0 1])); imshow(b) axis equal F(kl) = getframe; kl = kl + 1; anglestop = anglestop + pi/6; end end % Create last frames b=bitmaptext('Finish',a,[1 1],struct('FontSize',3,'Color',[0 0 0 1])); imshow(b) axis equal F(kl) = getframe; ```

## Create .avi file

Convert the movie to an avi

```cd('html'); movie2avi(F,'clock.avi', 'fps', 2, 'compression', 'None'); cd('..'); ```