Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Using a "for" loop to go through a number within a variable

1 Ansicht (letzte 30 Tage)
Woojae Kim
Woojae Kim am 9 Feb. 2019
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
I would like to change this sphagetti code into two for loops so that the first loop would go through "Segment_1_biceps_time" to "Segment_6_biceps_time" and second loop to go through "Trial_1_biceps_time" to "Trial_3_biceps_time". So I am guessing i should do that by changing part of the name of a variable to i and j.
(i.e. Segment_i_biceps_time & Trial_j_biceps_time , if i=1:6 and j = 1:3). But I am not sure how exactly i should go about this...
%% Segmenting time
Segment_1_biceps_time = Trial_1_biceps_time((Trial_1_biceps_time(:,1)> Segment_1_time_start) & (Trial_1_biceps_time(:,1)< Segment_1_time_end),:);
Segment_2_biceps_time = Trial_1_biceps_time((Trial_1_biceps_time(:,1)> Segment_2_time_start) & (Trial_1_biceps_time(:,1)< Segment_2_time_end),:);
Segment_3_biceps_time = Trial_1_biceps_time((Trial_1_biceps_time(:,1)> Segment_3_time_start) & (Trial_1_biceps_time(:,1)< Segment_3_time_end),:);
Segment_4_biceps_time = Trial_1_biceps_time((Trial_1_biceps_time(:,1)> Segment_4_time_start) & (Trial_1_biceps_time(:,1)< Segment_4_time_end),:);
Segment_5_biceps_time = Trial_1_biceps_time((Trial_1_biceps_time(:,1)> Segment_5_time_start) & (Trial_1_biceps_time(:,1)< Segment_5_time_end),:);
Segment_6_biceps_time = Trial_1_biceps_time((Trial_1_biceps_time(:,1)> Segment_6_time_start) & (Trial_1_biceps_time(:,1)< Segment_6_time_end),:);
Segment_1_biceps_time = Trial_2_biceps_time((Trial_2_biceps_time(:,1)> Segment_1_time_start) & (Trial_2_biceps_time(:,1)< Segment_1_time_end),:);
Segment_2_biceps_time = Trial_2_biceps_time((Trial_2_biceps_time(:,1)> Segment_2_time_start) & (Trial_2_biceps_time(:,1)< Segment_2_time_end),:);
Segment_3_biceps_time = Trial_2_biceps_time((Trial_2_biceps_time(:,1)> Segment_3_time_start) & (Trial_2_biceps_time(:,1)< Segment_3_time_end),:);
Segment_4_biceps_time = Trial_2_biceps_time((Trial_2_biceps_time(:,1)> Segment_4_time_start) & (Trial_2_biceps_time(:,1)< Segment_4_time_end),:);
Segment_5_biceps_time = Trial_2_biceps_time((Trial_2_biceps_time(:,1)> Segment_5_time_start) & (Trial_2_biceps_time(:,1)< Segment_5_time_end),:);
Segment_6_biceps_time = Trial_2_biceps_time((Trial_2_biceps_time(:,1)> Segment_6_time_start) & (Trial_2_biceps_time(:,1)< Segment_6_time_end),:);
Segment_1_biceps_time = Trial_3_biceps_time((Trial_3_biceps_time(:,1)> Segment_1_time_start) & (Trial_3_biceps_time(:,1)< Segment_1_time_end),:);
Segment_2_biceps_time = Trial_3_biceps_time((Trial_3_biceps_time(:,1)> Segment_2_time_start) & (Trial_3_biceps_time(:,1)< Segment_2_time_end),:);
Segment_3_biceps_time = Trial_3_biceps_time((Trial_3_biceps_time(:,1)> Segment_3_time_start) & (Trial_3_biceps_time(:,1)< Segment_3_time_end),:);
Segment_4_biceps_time = Trial_3_biceps_time((Trial_3_biceps_time(:,1)> Segment_4_time_start) & (Trial_3_biceps_time(:,1)< Segment_4_time_end),:);
Segment_5_biceps_time = Trial_3_biceps_time((Trial_3_biceps_time(:,1)> Segment_5_time_start) & (Trial_3_biceps_time(:,1)< Segment_5_time_end),:);
Segment_6_biceps_time = Trial_3_biceps_time((Trial_3_biceps_time(:,1)> Segment_6_time_start) & (Trial_3_biceps_time(:,1)< Segment_6_time_end),:);
  1 Kommentar
Stephen23
Stephen23 am 9 Feb. 2019
Bearbeitet: Stephen23 am 9 Feb. 2019
"But I am not sure how exactly i should go about this... "
Simple: redesign your code to use indexing.
Putting numbers into variable names is a sign that you are doing something wrong. And, as you are about to find out, it makes it impossible to efficiently and neatly process your data. Trying to access numbered variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
In contrast indexing is simple, neat, easy to debug, and much much more efficient than what you are trying to do. You should use indexing.

Antworten (0)

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by