Execute code faster for saving files with a matrix

1 visualizzazione (ultimi 30 giorni)
Hey guys! How are you. I need my code to execute faster. Here's the deal.
I have a matrix which is: datos2009(141214 x 11). I need to save files extracting information from that matrix, in the first column I have to save the data from 8, then the 10 and so on.
datos2009=[
8 29.24 5.96 10.6 0.06 0.69 22 72 -95.16 18.11 42.1
8 29.32 5.93 10.6 0.08 0.9 8 14 -95.16 18.11 42.1
8 30.19 6 10.6 0.06 0.69 14 28 -95.16 18.11 42.03
10 51.76 6.06 10.3 0.05 0.59 27 83 -95.16 18.11 41.42
10 51.84 5.66 10.3 0.05 0.59 14 40 -95.16 18.11 41.42
11 55.33 5.6 10.3 0.05 0.59 19 40 -95.16 18.11 41.37
11 55.68 5.9 10.3 0.07 0.8 15 39 -95.16 18.11 41.37]
prompt = 'Indique la ruta para guardar los archivos';
fpath=input(prompt,'s');
j=0;
k=datos2009(1);
header={'Costura', 'Kilometraje', 'Posición horaria', 'Espesor', 'Porcentaje de profundidad', 'Profundidad', 'Largo','Ancho', 'Longitud GPS', 'Latitud GPS', 'Elevación'};
cont=1;
values2009=zeros(50,11);
for i=1:size(datos2009)
j=datos2009(i);
if j==k
values2009(cont,:)=datos2009(i,:);
cont=cont+1;
else
zeroRow=~all(values2009, 2);
values2009(zeroRow,:)=[];
d = [header;num2cell(values2009)];
fileName=sprintf(strcat('File','%04d','_T_%d.csv'),k,k);
fullFileName=fullfile(fpath,fileName);
xlswrite(fullFileName,d);
values2009=zeros(50,11);
cont=1;
values2009(cont,:)=datos2009(i,:);
cont=cont+1;
k=j;
end
end
Any ideas how to run faster my code, taking into consideration that it takes around 12 hours to finish the job, about 5000 files are saved.
Thanks in advance.
  2 Commenti
Stephen23
Stephen23 il 9 Lug 2019
Modificato: Stephen23 il 9 Lug 2019
"Any ideas how to run faster my code"
Don't use xlswrite to write text files!
Do not convert between data classes (e.g. split numeric matrix into cell array).
If speed is your main concern, then write to file using low-level commands, e.g. fprintf.
Adrian Verdin
Adrian Verdin il 9 Lug 2019
Stephen Cobeldick thank you so much, it worked what you suggested (It just took 8 minutes instead of 14 hours), I ask you if you can move your comment to an answer in order to give you the credit.

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 9 Lug 2019
"Any ideas how to run faster my code"
Don't use xlswrite to write text files!
Do not convert between data classes (e.g. do not split numeric matrix into cell array).
If speed is your main concern, then write to file using low-level commands, e.g. fprintf.

Più risposte (0)

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Prodotti


Release

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by