Update multiple tables in loops
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Siddharth Bhutiya
am 1 Apr. 2022
Can you show a sample output of what you want the final table to look like?
Antworten (1)
Chetan
am 22 Sep. 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:
- Prior to starting the loop, create an empty cell array to store the data for each game.
- 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Web Services finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!