How to select specific data and create a table

2 次查看(过去 30 天)
Hi everyone,
I have been looking for some information in the answers and I have an idea of how to perform my code but I'm still lacking some knowledge and I get lost.
I have an Excel file (find it attached) which has in the first column the name of the runners of the 5000 m in the olympic games with the heats (runs) that everyone of them did and next to it I have the time they did.
I want to:
  • Read the name of the first runner (Hagos Gebrhiwet) and then the time from heat 1 to heat 5 but the code needs to check what is the last heat and then decide that after heat 5 the next input is a new runner.
  • Read the name of runner 2 (Albert Kibichii Rop) and then the time of his 2 heats and decide that after that there is a new runner.
  • When it finishes reading all the data I want to output it in a table like the second picture I have attached.
You will find either the input and ouputs in the attached Excel file.
The main purpose of my question is to know the best way to import the data (xlsread works fine) and then manage to put the data as I would like the simplest possible way.
Many thanks in advance
Captura.JPG
Captura.JPG

采纳的回答

Guillaume
Guillaume 2019-3-12
One approach:
[~, ~, data] = xlsread('Example_Runners.xlsx'); %read the whole lot
data = data(2:end, :); %remove header
isrunner = ~startsWith(data(:, 1), 'Heat'); %identify the rows that are not heat
runners = repelem(data(isrunner, 1), diff(find([isrunner;true]))-1); %replicate runner for each heat
t = [table(runners), cell2table(data(~isrunner, :), 'VariableNames', {'heat', 'time'})];
unstack(t, 'time', 'heat')
  1 个评论
David Rojas Blanco
David Rojas Blanco 2019-3-12
Hi Guillaume,
This absolutely worked! Thank you very much. I was able to understand a bit better how to do this kind of operations.
Many thanks

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by