# Plot for the Top10 Players in Matlab Cody

• Created by: J.R.! Menzinger

### J.R.! Menzinger (view profile)

• Latest result: Plot created
• Created on: 21 Aug 2013
• Liked: 2 times

This is a general solution to solve the problem of "unknown" players.

%% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %%

% Top Players in Cody
%   time vector is: time1748
%   data vector is: data1748

%% actual data for the legend:
url = 'http://www.mathworks.com/matlabcentral/cody/players';

nameStart = regexp(html, '<h2 class="full_name">','end')+1;

nameEnd = regexp(html, '</h2>','start')-1;

% for i =1:numel(nameStart)
for iP = 1:10
% disp(html(nameStart(i):nameEnd(i)))
st = html(nameStart(iP):nameEnd(iP));

aux = regexp(st, 'players/(\d*)', 'tokens');
data{iP,1} = str2num(aux{1}{1}); % the IDS of the top players

aux = regexp(st, '>(.*)<', 'tokens');
data{iP,2} = aux{1}{1}; % the NAMES of the top players

aux = regexp(st, 'Score: (\d*)', 'tokens');
data{iP,3} = str2num(aux{1}{1}); % the SCORES of the top players

end

playerIdsTop10 = [data{:,1}];
playerNameTop10 = data(:,2);
playerScoreTop10 = [data{:,3}];

%% use only the data from the last 90 days:
tDays = 90;
data1748 = data1748(end-(0:tDays+1),:);
time1748 = time1748(end-(0:tDays+1),:);

%% get the scores of each player:
playerIds = data1748(:,1:2:end)';
playerScores = data1748(:,2:2:end)';

allPlayers = unique(playerIds(:))';
nP = numel(allPlayers);
nT = numel(time1748);
scores = nan(nT,nP);

allPlayers = [playerIdsTop10 allPlayers(~ismember(allPlayers,playerIdsTop10))];

for iP = 1:nP

timeID = playerIds == allPlayers(iP);

actualScore = playerScores(timeID);

nAS = numel(actualScore);
nS = size(scores,1);

scores(any(timeID),iP) = actualScore;

end

scores = [playerScoreTop10 ;  scores(:,1:10)];
time1748 = [now ; time1748];

%% plot
figure('Position',[440   28   560   970])

h = plot(time1748,scores, '-');
hold on
hO = plot(time1748(1),scores(1,:), 'd');
datetick('x','dd mmm ''yy','keepticks')

grid on

set([h(1:10) ; hO], 'LineWidth',2)
set(h(8:10), 'LineStyle','--')
set(h(11:end), 'LineStyle',':')
set(hO, {'Color'},get(h,'Color') , ...
'MarkerSize',13)

yTick = get(gca, 'YTick');
yTickLabel = cellstr(num2str(yTick'/1000, '%2.0fk'));

xLim = [min(time1748) max(time1748)];
xLim(2) = floor(now);

set(gca, 'XLim',xLim+[0 15] , ...
'XTick',[fliplr(xLim(2):-15:xLim(1)) xLim(2)+5] , ...
'XTickLabelRotation',-40 , ...
'YAxisLocation','right' , ...
'YTickLabel',yTickLabel)

datetick('x','dd mmm ''yy', 'keepticks')

xTick = get(gca, 'XTick');
xTickLabel = cellstr(get(gca, 'XTickLabel'));

set(gca, 'XTick',xTick(2:end-1) , ...
'XTickLabel',xTickLabel(2:end-1))

title('\bfTop10\rm on MATLAB Cody', 'FontSize',20)
xlabel({'Last 3 months of Cody' ; ' ' }, 'FontSize',16)
ylabel('Score', 'FontSize',16)

%% Legend:

playerNameTop10{1} =   ['\fontsize{16}\bf' num2str(1, '#%02d') ' ' playerNameTop10{1} '  (' num2str(playerScoreTop10(1)) ' points)'];

for iP = 2:10
playerNameTop10{iP} = ['\fontsize{13}' num2str(iP, '#%02d') ' ' playerNameTop10{iP} '  (' num2str(playerScoreTop10(iP)) ' points)'];
end

leg = legend(playerNameTop10{:} , ...
'Location','SouthOutside');
Tags: