How do I create a for loop for the this example?

accelX_cd1 = accelX(i_start_cd1:i_end_cd1);
accelY_cd1 = accelY(i_start_cd1:i_end_cd1);
accelZ_cd1 = accelZ(i_start_cd1:i_end_cd1);
accelX_cd2 = accelX (i_start_cd2:i_end_cd2);
accelY_cd2 = accelY (i_start_cd2:i_end_cd2);
accelZ_cd2 = accelZ (i_start_cd2:i_end_cd2);
accelX_cd3 = accelX (i_start_cd3:i_end_cd3);
accelY_cd3 = accelY (i_start_cd3:i_end_cd3);
accelZ_cd3 = accelZ (i_start_cd3:i_end_cd3);
accelX_cd4 = accelX (i_start_cd4:i_end_cd4);
accelY_cd4 = accelY (i_start_cd4:i_end_cd4);
accelZ_cd4 = accelZ (i_start_cd4:i_end_cd4);
accelX_cd5 = accelX (i_start_cd5:i_end_cd5);
accelY_cd5 = accelY (i_start_cd5:i_end_cd5);
accelZ_cd5 = accelZ (i_start_cd5:i_end_cd5);
accelX_cd6 = accelX (i_start_cd6:i_end_cd6);
accelY_cd6 = accelY (i_start_cd6:i_end_cd6);
accelZ_cd6 = accelZ (i_start_cd6:i_end_cd6);

1 comentario

Ghazwan
Ghazwan el 15 de Oct. de 2022
You need to arrange them in arrays, with the columns (or rows) being the variables you need to loop.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 15 de Oct. de 2022

0 votos

The solution is easy, if you do not hide the index in the name of the variable as in i_end_cd1, i_end_cd2, ... Use a vector instead and real indices: i_end_cd(1), i_end_cd(2), ...

5 comentarios

Francis Trombini
Francis Trombini el 15 de Oct. de 2022
Editada: Jan el 15 de Oct. de 2022
Thank you for your reply.
I adapted this part as you suggested "i_end_cd(1), i_end_cd(2), ..." and tried to implement the following code:
for i = 1:6
accelX_cd(i) = accelX(i_start_cd(i):i_end_cd(i));
accelY_cd(i) = accelY(i_start_cd(i):i_end_cd(i));
accelZ_cd(i) = accelZ(i_start_cd(i):i_end_cd(i));
end
But, the following message appears: "Subscripted assignment dimension mismatch."
What do you suggest?
Please use the tools to format code in the forum. This increases the readability.
Use the debugger to identify such problems. Type in the command window:
dbstop if error
Then run the code again. When Matlab stops in the failing line, check the values of the expressions. You did not post the complete error message (a copy is better than just a fragment), so I guess it is this line:
accelX_cd(i) = accelX(i_start_cd(i):i_end_cd(i));
Evaluate the right side in the command window:
accelX(i_start_cd(i):i_end_cd(i))
size(accelX(i_start_cd(i):i_end_cd(i)))
This seems to be a vector. But on the left side there is a scalar only: accelX_cd(i).
The message means, that you cannot assign a vector to a scalar.
If you explain the purpose of the code, I could suggest solutions.
Thanks again for trying to help me.
I have a file (5280, 3) with linear acceleration data on the X-axis (first column), Y-axis (second column), and Z-axis (third column). Initially, I plot the first column (X-axis) and select (cut) 6 segments on that signal (please see the attached figure). I named these segments condition 1 (cd1), condition 2 (cd2), condition 3 (cd3), condition 4 (cd4), condition 5 (cd5) and condition 6 (cd6). After selection, each of these segments of the first column (X-axis) has an index referring to the data’s beginning and end. Thus, I need a loop to adopt the index of the beginning and end of each of the 6 conditions (from the first column) to define that same data range in the second (Y axis) and third column (Z axis) of this file.
"I named these segments condition 1 (cd1), condition 2 (cd2), condition 3 (cd3), condition 4 (cd4), condition 5 (cd5) and condition 6 (cd6)" - this is the problem already. Use arrays instead: Cond(1), Cond(2), ... I avoid to use "cd" as name of a variable, because this collides with the cd() command.
If you work with arrays, running a loop is trivial:
for k = 1:6
disp(Cond(k)) % Or whatever
end
Thank you for your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Preguntada:

el 15 de Oct. de 2022

Comentada:

el 18 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by