Retrieve Team Standings for the Current Year

This example shows how to retrieve basketball team standing data from STATS.com for the current year.

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

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

Connect to STATS.com

Create a STATS.com connection sBasketball for retrieval of statistical basketball data for the sport league named 'nba'. Set up the connection to retrieve team standing data by specifying the query parameter 'Resource' as 'standings'.

sBasketball = statsllc('se33pc658r9abcahm4a9j93p','frvaBcb33y', ...
    'DataType','stats','LeagueAbbreviation','nba', ...
    'Resource','standings','SportName','basketball', ...
    'VersionNumber','v1');

Retrieve Team Standings Data

Retrieve basketball team standings data using sBasketball.

data = fetch(sBasketball);

Retrieve the conference, division, and team data by accessing the nested structure data. To access each layer of the data, create a nested loop. Convert the returned structure data to tables. Create the table standingsTable that contains the team standings data. Display the data in the Command Window.

numConferences = length(data.apiResults.league.season.eventType.conferences);

% Iterate through each conference
for i = 1:numConferences
    numDivisions = length(data.apiResults.league.season.eventType.conferences(i).divisions);
  
    % Iterate through each division
    for j = 1:numDivisions
        
        % Access team data in each division
        t = struct2table(data.apiResults.league.season.eventType.conferences(i).divisions(j).teams);
        numTeams = length(data.apiResults.league.season.eventType.conferences(i).divisions(j).teams);
        recordTable = [];
        
        % Iterate through each team
        for k = 1:numTeams
            divisionStatusTable = struct2table(data.apiResults.league.season.eventType.conferences(i).divisions(j).teams(k).division);
            recordTable = [recordTable;struct2table(data.apiResults.league.season.eventType.conferences(i).divisions(j).teams(k).record) ...
                table(divisionStatusTable.gamesBehind, ...
                    'VariableNames',{'GamesBack'})];
        end

        % Create table of team standing data
        standingsTable = table(strcat(t.location,strcat('.',t.nickname)), ...
            recordTable.wins,recordTable.losses,recordTable.percentage, ...
            recordTable.GamesBack,'VariableNames', ...
            {'Team','Wins','Losses','Percentage','GamesBehind'});
        
        % Display team standing data to the command line
        disp([data.apiResults.league.season.eventType.conferences(i).name ...
            ' ' data.apiResults.league.season.eventType.conferences(i).divisions(j).name])
        disp(' ')
        disp(standingsTable)
    end
end
Eastern Conference Atlantic Division
 
            Team            Wins    Losses    Percentage    GamesBehind
    ____________________    ____    ______    __________    ___________

    'Toronto.Raptors'       56      26        .683           0         
    'Boston.Celtics'        48      34        .585           8         
    'New York.Knicks'       32      50        .390          24         
    'Brooklyn.Nets'         21      61        .256          35         
    'Philadelphia.76ers'    10      72        .122          46         
...

The Command Window displays the basketball team standings for each division. The column names are:

  • Team — Basketball team name

  • Wins — Number of wins

  • Losses — Number of losses

  • Percentage — Percentage of games won

  • GamesBehind — Number of games behind

See Also

|

Related Topics

External Websites