Since you are new, let us show you how Trendy works.

Close-button

Plot for the Top10 Players in Matlab Cody

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

Plot Image
%% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% 

% 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';
html = urlread(url);

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

%% Add actual value:
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,:), 'o');
datetick

grid on

set([h(1:10) ; hO], 'LineWidth',2)
set(h(8:10), 'LineStyle','--')
set(h(11:end), 'LineStyle',':')

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

set(gca, 'XLim',[min(time1748) max(time1748)+5] , ...
    'YAxisLocation','right' , ...
    'YTickLabel',yTickLabel)

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:

Add Tags


0 comments