If statement in cell array; skip row if it equals certain value

Hi all, I have a cell structure named RigidBodies, which contains five structures. These structures contain from 1 to 4 cells named Marker + number. Each Marker contains three columns with x,y and z data through time. I wrote a loop to subtract a specific value (eg. x0, y0 and z0) from the x, y and z values if the Marker cell is not empty and if the values of x,y and z are different than 0. If the values are 0, I want the rows to stay as they are. Below is my code, which does not report any error and neither does its job - it does not subtract the desired value (sample data is attached). What is wrong? Thank you.
Jurij Hladnik
Subtract = RigidBodies(1).Marker4(1,:);
for c = 1 : length(RigidBodies);
nmarkers = fieldnames(RigidBodies(c));
nmarkers = sum(strcmp(cellfun(@(x) x(1:end-1), nmarkers, 'un', 0), 'Marker'));
for d = 1 : nmarkers
~isempty(RigidBodies(c).(sprintf('Marker%d',d)))
for f = 1 : length (RigidBodies(c).(sprintf('Marker%d',d)))
if (RigidBodies(c).(sprintf('Marker%d',d))(f,:)==[0,0,0])
continue
else
RigidBodies(c).(sprintf('Marker%d',d))(f,:) = RigidBodies(c).(sprintf('Marker%d',d))(f,:) - Subtract;
end
end
end

Respuestas (1)

Thorsten
Thorsten el 23 de Nov. de 2015
Editada: Thorsten el 24 de Nov. de 2015
Subtract = RigidBodies(1).Marker4(1,:);
for c = 1 : numel(RigidBodies)
nmarkers = fieldnames(RigidBodies(c));
for d = 1:numel(nmarkers)
x = getfield(RigidBodies, {c}, nmarkers{d});
if ~isempty(x)
idx0 = find(sum(x,2) == 0);
x = bsxfun(@minus, x, Subtract);
for e = idx0, x(e, :) = [0, 0, 0]; end
RigidBodies = setfield(RigidBodies, {c}, nmarkers{d}, x);
end
end
end

3 comentarios

Dear Thorsten,
thank you for your code. Unfortunately it does not do the job as I wish. The last for loop of your code is supposed to set back the zero lines to (0,0,0) so the subtraction would not affect them. But this loop sets the first lines to zero - not the right lines. How can I apply this for loop only to the rows, which were 0,0,0 before the subtraction? Thank you. Regards,
JH
Dear JH,
there was an error in my code. I corrected it, it should work now.
Still doesn't work. I get the following error: "Subscripted assignment dimension mismatch."

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

JH
el 23 de Nov. de 2015

Comentada:

JH
el 24 de Nov. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by