Update multiple tables in loops

1 visualización (últimos 30 días)
Lincoln Miller
Lincoln Miller el 31 de Mzo. de 2022
Respondida: Chetan el 22 de Sept. de 2023
I have a while loop running this code to pull score board data from sports games. The goal is to have it not repeat the tables and just update them. There is a while loop to be able to select different sports.
Here is my code-
clear
clc
%initialize
g=1; %first game
t= 1; %teams
url = 'http://site.api.espn.com/apis/site/v2/sports/basketball/mens-college-basketball/scoreboard';
bball = webread(url);
events= getfield(bball,'events');
e = size(events,1); %for max number of games that day
for g = 1:e
Games{g}= getfield(events, {g} , 'competitions');
status = getfield(events,{g} , 'status');
half = getfield(status,'period');
clock = getfield(status,'displayClock');
strper = num2str(half);
for t = 1:2
teams = getfield(Games{g},'competitors');
name = getfield(teams , {1},'team'); %home
dishpname= getfield(name,'abbreviation'); %homename
hscore = getfield(teams, {1},'score'); %homescore
aname = getfield(teams , {2},'team'); %away
dispaname= getfield(aname,'abbreviation'); %away name
ascore = getfield(teams, {2},'score');
t = t+1;
strhscore = num2str(hscore);
strascore=num2str(ascore);
end
datacell={half,dishpname,hscore;clock,dispaname,ascore};
Gamenumb=g;
Table= cell2table(datacell,"RowNames",{'home','Away'},"VariableNames",{'Half','Team','Score'})
g=g+1;
end
  2 comentarios
Siddharth Bhutiya
Siddharth Bhutiya el 1 de Abr. de 2022
Can you show a sample output of what you want the final table to look like?
Lincoln Miller
Lincoln Miller el 6 de Abr. de 2022
Sorry I just saw your respones. here is a sample. I can form this from the code, i just would like the table to update and not repeat itself.

Iniciar sesión para comentar.

Respuestas (1)

Chetan
Chetan el 22 de Sept. de 2023
I understand that you are trying to access the API data and generate a single output table by processing selected information from the data. To achieve this, I suggest making the following modifications to your code:
  1. Prior to starting the loop, create an empty cell array to store the data for each game.
  2. Inside the loop, instead of creating a new cell array named "datacell" for each game, you should also append the data to the "datacell" cell array.
Here is the modified code for this:
clear
clc
%initialize
url = 'http://site.api.espn.com/apis/site/v2/sports/basketball/mens-college-basketball/scoreboard';
bball = webread(url);
events = getfield(bball, 'events');
e = size(events, 1); %for max number of games that day
% Create an empty cell array to store all data
allData = {};
for g = 1:e
Games{g} = getfield(events, {g}, 'competitions');
status = getfield(events, {g}, 'status');
half = getfield(status, 'period');
clock = getfield(status, 'displayClock');
strper = num2str(half);
teams = getfield(Games{g}, 'competitors');
name = getfield(teams, {1}, 'team'); %home
dishpname = getfield(name, 'abbreviation'); %homename
hscore = getfield(teams, {1}, 'score'); %homescore
aname = getfield(teams, {2}, 'team'); %away
dispaname = getfield(aname, 'abbreviation'); %away name
ascore = getfield(teams, {2}, 'score');
allData{end+1, 1} = half;
allData{end, 2} = dishpname;
allData{end, 3} = hscore;
allData{end, 4} = clock;
allData{end, 5} = dispaname;
allData{end, 6} = ascore;
end
% Convert allData to a table
Table = cell2table(allData , 'VariableNames', {'Half', 'TeamHome', 'HomeScore','Clock','AwayTeam','AwayScore'});
I hope these suggestions help you resolve the issue you are facing.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by