Improve Speed of Code (especially for loop)

I want to analyse a log file that i created my self, basically the content is a CSV table with 5 columns {Timeindex , ActorName, X, Y, Z}. (There are only 8 Actors)
for that I made my first matlab function, in general matlab experience, ever xD which is this:
function Auswertung (matrix)
va = size(matrix);
bonePlotsX= zeros(8,2,100000);
bonePlotsY= zeros(8,2,100000);
bonePlotsZ= zeros(8,2,100000);
%Current row in which to write for each of the roads.
is={1,1,1,1,1,1,1,1};
for i=1:1:(va(1))
currentIndex=1;
var = matrix{i,2};
var= strtrim(var);
switch(var)
case "Hips"
currentIndex=1;
case "Spine"
currentIndex=2;
case "LeftUpperLeg"
currentIndex=3;
case "LeftLowerLeg"
currentIndex=4;
case "LeftFoot"
currentIndex=5;
case "RightUpperLeg"
currentIndex=6;
case "RightLowerLeg"
currentIndex=7;
case "RightFoot"
currentIndex=8;
end
bonePlotsX(currentIndex,1,is{currentIndex})=matrix{i,1};
bonePlotsX(currentIndex,2,is{currentIndex})=matrix{i,3};
bonePlotsY(currentIndex,1,is{currentIndex})=matrix{i,1};
bonePlotsY(currentIndex,2,is{currentIndex})=matrix{i,4};
bonePlotsZ(currentIndex,1,is{currentIndex})=matrix{i,1};
bonePlotsZ(currentIndex,2,is{currentIndex})=matrix{i,5};
is{currentIndex}= is{currentIndex}+1;
end
%First Bone Print
figure
subplot(3,1,1)
plot( bonePlotsX(1,1),bonePlotsX(1,2));
title("X of Hips");
subplot(3,1,1)
plot( bonePlotsY(1,1),bonePlotsY(1,2));
title("Y of Hips");
subplot(3,1,1)
plot( bonePlotsZ(1,1),bonePlotsZ(1,2));
title("Z of Hips");
end
The problem is it takes awfully long to complete the for loop. The sample file has just about 400 000 Lines, so not something I would consider much for a PC. How can I optimize this Loop? I tried pre allocating the bone matrices, 100 000 should be enough for even the longer logfiles.
As you see the end result should be a plot for all of the bones (in the code its just one for testing purposes)

1 comentario

KSSV
KSSV el 12 de Abr. de 2017
You can achieve the above without using loop.
1. Scan your csv file.
2. Get the indices of your required string (Hip, Spine, etc).
3. With the obtained indices, pick your required data.

Iniciar sesión para comentar.

 Respuesta aceptada

KSSV
KSSV el 12 de Abr. de 2017
You may check this example code:
[num,txt,raw] = xlsread('demo.xls') ;
name = txt ;
%%find nose
idx = strfind(name,'nose') ;
idx1 = strfind(name, 'nose');
idx1 = find(not(cellfun('isempty', idx1)));
%%nose data
data1 = num(idx1,:) ;
Like above you can follow for other required data.

Más respuestas (0)

Productos

Etiquetas

Preguntada:

el 12 de Abr. de 2017

Respondida:

el 12 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by