Select best time in an array

1 visualización (últimos 30 días)
Franco Mazzaro
Franco Mazzaro el 8 de Jun. de 2022
Respondida: Peter Perkins el 13 de Jun. de 2022
Hi everyone! So i have this function that creates a .tx with drivers and their respective lap times:
function txtcreator_v2(drivers_scuderias,file)
%TXTCREATOR asigna un tiempo de vuelta a cada conductor y escribe los
%resultados en un archivo .txt.
%driversscuderias debe ser un string array
%file es el nombre del archivo que queremos crear
minutes = randi(2,1,100); seconds = randi(59,1,100); milliseconds=randi(999,1,100);
%generador de tiempo
fid = fopen(file,'w+');
for i=1:length(minutes)
fangio = randi(20,1,1);
fprintf(fid,'%s;%s;%d:%02d.%03d\n',drivers_scuderias(fangio,1),drivers_scuderias(fangio,2),minutes(i),seconds(i),milliseconds(i));
end
fclose(fid);
end
and then a script that reads the text file and structures it:
driv_scu= ["LEC" "SAI" "BOT" "ZHO" "ALB" "LAT" "VER" "PER" "HAM" ...
"RUS" "NOR" "RIC" "MSC" "MAG" "STR" "VET" "GAS" "TSU" "ALO" "OCO";"ferrari" "ferrari" "alfa romeo" "alfa romeo" "williams" "williams" "red bull" "red bull" "mercedes" "mercedes"...
"mclaren" "mclaren" "haas" "haas" "aston martin" "aston martin" "alpha tauri" "alpha tauri" "alpine" "alpine"]';
txtcreator_v2(driv_scu,'laptimes.txt')
fid = fopen("laptimes.txt");
parrilla = textscan(fid,'%s %s %s','delimiter',';');
fclose(fid);
domenicalli=vertcat( parrilla{1} );
senna=vertcat( parrilla{2} );
prost=vertcat( parrilla{3} );
ayrton= [domenicalli senna prost];
The thing is, i want ayrton to only have the best lap times of each pilot. I mean deleting the repeated names based on their lap times (if hamilton is repited two times, i only keep the "hamilton" with the best lap)
Any clues?

Respuesta aceptada

Peter Perkins
Peter Perkins el 13 de Jun. de 2022
You are making your life more difficult using fopen etc. Use writetable and readtable:
>> laptimes = readtable("laptimes.txt","ReadVariableNames",false);
>> laptimes.Properties.VariableNames = ["Driver" "Team" "Time"];
>> laptimes.Time = duration(laptimes.Time,"Format","mm:ss.SSS")
laptimes =
100×3 table
Driver Team Time
_______ ________________ _________
{'SAI'} {'ferrari' } 01:44.985
{'GAS'} {'alpha tauri' } 02:53.559
{'NOR'} {'mclaren' } 01:15.933
{'MAG'} {'haas' } 01:08.720
{'ALB'} {'williams' } 01:14.484
{'NOR'} {'mclaren' } 02:21.639
{'STR'} {'aston martin'} 02:17.887
{'OCO'} {'alpine' } 02:55.199
{'HAM'} {'mercedes' } 02:04.395
{'SAI'} {'ferrari' } 01:35.992
{'SAI'} {'ferrari' } 02:10.402
{'MSC'} {'haas' } 01:50.659
{'VET'} {'aston martin'} 02:10.901
{'MAG'} {'haas' } 02:30.995
{'VER'} {'red bull' } 02:59.653
{'ALO'} {'alpine' } 01:21.109
{'NOR'} {'mclaren' } 01:03.037
{'OCO'} {'alpine' } 02:13.618
{'SAI'} {'ferrari' } 02:24.567
{'ALB'} {'williams' } 02:20.962
: : :
{'VER'} {'red bull' } 02:37.751
{'GAS'} {'alpha tauri' } 02:16.368
{'ALB'} {'williams' } 01:27.941
{'RIC'} {'mclaren' } 02:50.018
{'ALO'} {'alpine' } 02:12.829
{'LEC'} {'ferrari' } 02:18.626
{'SAI'} {'ferrari' } 02:29.539
{'LEC'} {'ferrari' } 01:20.650
{'MAG'} {'haas' } 02:48.726
{'RIC'} {'mclaren' } 01:59.095
{'BOT'} {'alfa romeo' } 02:10.877
{'VET'} {'aston martin'} 02:14.015
{'MSC'} {'haas' } 02:42.295
{'SAI'} {'ferrari' } 01:23.180
{'SAI'} {'ferrari' } 02:58.926
{'BOT'} {'alfa romeo' } 01:58.069
{'VET'} {'aston martin'} 01:38.581
{'SAI'} {'ferrari' } 02:51.637
{'ALB'} {'williams' } 02:24.651
{'ALB'} {'williams' } 01:38.864
Display all 100 rows.
If the file had been written with column headings, that code would have been even shorter.
At that point, what you want is a one-liner:
>> besttimes = varfun(@min,laptimes,"GroupingVariables","Driver","InputVariables","Time")
besttimes =
20×3 table
Driver GroupCount min_Time
_______ __________ _________
{'ALB'} 6 01:14.484
{'ALO'} 7 01:21.109
{'BOT'} 7 01:15.051
{'GAS'} 7 01:56.890
{'HAM'} 3 01:35.276
{'LAT'} 6 01:18.880
{'LEC'} 5 01:20.650
{'MAG'} 8 01:08.720
{'MSC'} 3 01:31.334
{'NOR'} 5 01:03.037
{'OCO'} 4 01:57.805
{'PER'} 1 02:34.701
{'RIC'} 6 01:34.114
{'RUS'} 3 01:30.452
{'SAI'} 9 01:23.180
{'STR'} 3 02:17.887
{'TSU'} 2 01:34.514
{'VER'} 6 01:14.059
{'VET'} 7 01:01.031
{'ZHO'} 2 01:26.962

Más respuestas (0)

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by