if statement didn't executed in a loop
Ältere Kommentare anzeigen
Hi all, I have problem on the if statement that inside can't be executed which makes other operations in the code cannot be run. My code is as follow, the problems is occurred at the "if isequal(time(i),off_tt)" parts where the variables 'k' didn't increment.
k = 0; on_t = 2;off_t = 2;
for i=1:length(time)
on_tt = (on_t*(k+1))+(off_t*k)
off_tt = (on_t*(k+1))+(off_t*(k+1))
if(time(i) ~= off_tt)
if (on_t == 0)
ref_speed_left = [ref_speed_left, 0];
ref_speed_right = [ref_speed_right, 0];
elseif (off_t == 0)
ref_speed_left = [ref_speed_left, ref_speedR];
ref_speed_right = [ref_speed_right, ref_speedL];
elseif (time(i) < on_tt) %ans == 0
ref_speed_left = [ref_speed_left, ref_speedR];
ref_speed_right = [ref_speed_right, ref_speedL];
elseif (time(i)>= on_tt && time(i) < off_tt+1)
if isequal(time(i),off_tt) %problems at this if statement
k=k+1; %k didn't increment when it
end %met the requirement
ref_speed_left = [ref_speed_left, 0]
ref_speed_right = [ref_speed_right, 0]
end
end
end
May I know any solutions for this, your helps is much appreciate. Thanks you.
2 Kommentare
Azzi Abdelmalek
am 13 Dez. 2012
Can you post values of ref_speedR and others variables, to allow testing your code
Shawn Chang
am 13 Dez. 2012
Bearbeitet: Shawn Chang
am 13 Dez. 2012
Antworten (3)
Ilham Hardy
am 13 Dez. 2012
for i=1:length(time)
k=i %correction
on_tt = (on_t*(k+1))+(off_t*k)
off_tt = (on_t*(k+1))+(off_t*(k+1))
You declare k=0, therefore k is 0.
1 Kommentar
Shawn Chang
am 13 Dez. 2012
Azzi Abdelmalek
am 13 Dez. 2012
Bearbeitet: Azzi Abdelmalek
am 13 Dez. 2012
0 Stimmen
Are you sur the condition if isequal(time(i),off_tt) is true? I tested your code, the condition is true only for i=80
8 Kommentare
Shawn Chang
am 13 Dez. 2012
Bearbeitet: Shawn Chang
am 13 Dez. 2012
Azzi Abdelmalek
am 13 Dez. 2012
Bearbeitet: Azzi Abdelmalek
am 13 Dez. 2012
Ok, when time(80) = 4, off_tt = 4, you forget that your code is executed when the condition if(time(i) ~= off_tt) is true, which it is not in this case.
Shawn Chang
am 13 Dez. 2012
Azzi Abdelmalek
am 13 Dez. 2012
In this case the condition isequal(time(i),off_tt) occurs one time, look at k, (k=1);
Shawn Chang
am 13 Dez. 2012
Azzi Abdelmalek
am 13 Dez. 2012
Do you want to increment k each increment of i, if not tell us what is the condition?
Shawn Chang
am 13 Dez. 2012
Azzi Abdelmalek
am 13 Dez. 2012
you have then to revise your code
Roger Stafford
am 13 Dez. 2012
Shawn, look at the third line from the beginning of the for-loop:
if(time(i) ~= off_tt)
It is an outer condition that must be satisfied if you are to enter the if-elseif-elseif-end sequence. This means you cannot reach the
if isequal(time(i),off_tt)
test with equal values of time(i) and off_tt. Hence the 'isequal' test will always be false and k can never be incremented.
I am curious as to why you used 'isequal' at that point. It is intended for testing equality between entire arrays and suggests that you may have meant something else in your code here.
Roger Stafford
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!