Extracting time from a string
Ältere Kommentare anzeigen
Hi could i extract the time from this line, which is saved as a text
90 83 3010 Kobe Soto Lewisville 18:19.9 5:52/M 9
the desired extraction is : 18:19.9,
now this will have to repeat for other 300 similar lines like the one above.
1 Kommentar
Walter Roberson
am 27 Mai 2020
What is known to be consistent? Will it always be in the 7th field? Will it always be in the 3rd last field? Is it guaranteed that there will be a decimal point in the time? Will the time have leading 0 for before 12:00 ?
Antworten (1)
Cris LaPierre
am 28 Mai 2020
Bearbeitet: Cris LaPierre
am 28 Mai 2020
I used to be very involved in XC and track and field, so of course I had to help you out. Here you go:
% Scrape the data from the webpage
url = 'https://tx.milesplit.com/meets/242182/results/474095/raw#.Xs8w-8ApCMo';
code = webread(url);
str = extractHTMLText(code);
% Read in and format the data
varNames = ["Place" "AdjPlace" "Bib No" "Name" "Team" "Time" "Pace" "YR"];
data = textscan(str(6:end),'%f %q %f %q %C %{mm:ss.S}T %{mm:ss}T %*q %f',...
'Delimiter',{'\t','/'},...
"EndOfLine",'\n',...
"HeaderLines",5,...
"TextType",'string');
% Place the results in a table
results = cell2table(data);
results = varfun(@(x) x{:},results);
results.Properties.VariableNames = varNames
If you are new to tables, take a minute to go through this doc page. You can also see an introduction to accessing data in a table in this video from the Exploratory Data Analysis with MATLAB course on Coursera.
In short, you use dot notation to indicate which table variable to use. For example
results.Time(90)
ans =
18:19.9
If you want the entire row, use the (row, column) syntax instead:
results(90,:)
ans =
Place AdjPlace Bib No Name Team Time Pace YR
_____ ________ ______ ___________ __________ _______ _____ __
90 "83" 3010 "Kobe Soto" Lewisville 18:19.9 05:52 9
Kategorien
Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!