Main Content

Compare Player Salaries by Injury Status

This example shows how to retrieve salary and injury data from for individual baseball players. To compare salaries of players with and without injuries, visualize the data in a scatter plot.

This example requires a API key and secret pass code for baseball data. For details about your credentials for data access, contact

To access the code for this example, enter edit MLBSalaryVsInjuryExample.m.

Connect to

Create a connection sBaseball for retrieval of statistical baseball data for the sport league named 'mlb'. Set up the connection to retrieve salary data by specifying the query parameter 'Resource' as 'salaries'.

sBaseball = statsllc('gkfrq6fxabcehmn2yctrc6j5','qYR5abCQgc', ...
    'DataType','stats','LeagueAbbreviation','mlb', ...
    'Resource','salaries','SportName','baseball', ...

Retrieve Baseball Player Salary and Injury Data

Retrieve individual baseball player salaries data using sBaseball.

data = fetch(sBaseball);

To retrieve the individual baseball player data playerData, access the nested structure data. Create a salary data table salaryData by retrieving the structure fields in playerData.

playerData = [data.apiResults.league.season.salaries.player];

salaryData = table((1:data.recordCount)',[playerData.playerId]', ...
    {playerData.firstName}',{playerData.lastName}', ...
    [data.apiResults.league.season.salaries.salary]', ...
    false(data.recordCount,1), ...
    'VariableNames',{'Record','PlayerId','FirstName', ...

salaryData contains these column names:

  • Record — Record number

  • PlayerId — Baseball player identifier

  • FirstName — Baseball player first name

  • LastName — Baseball player last name

  • Salary — Baseball player salary

  • Injured — Baseball player injured status

Baseball player injured status is a placeholder for the injury data.

To retrieve the injury data, change the query parameter Resource value from 'salaries' to 'participants' in the connection sBaseball. Retrieve the individual baseball player data using sBaseball.

sBaseball.Resource = 'participants';

data = fetch(sBaseball);

Loop through the resulting nested structure data for each player. Set the injured status in salaryData for those players that are injured.

numPlayers = data.recordCount;
for i = 1:numPlayers
    playerId = data.apiResults.league.players{i}.playerId;
    salaryData.Injured(salaryData.PlayerId == playerId) ...
        = data.apiResults.league.players{i}.isInjured;

Visualize Baseball Player Salary and Injury Data

Create a scatter plot of the salary for each baseball player.

sInjured = scatter(salaryData.Record(salaryData.Injured), ...
    salaryData.Salary(salaryData.Injured),'Marker','+', ...
hold on
sNotInjured = scatter(salaryData.Record(~salaryData.Injured), ...
    salaryData.Salary(~salaryData.Injured),'Marker','O', ...
hold off
sInjured.MarkerFaceColor = 'flat'
grid on
b = gca
title('Player Salary Comparison')
legend({'Player with Injury','Player Without Injury'},'Location','best')

The scatter plot of baseball players and their salaries displays. Compare the salaries for injured and uninjured baseball players.

See Also


Related Topics

External Websites