Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Help tidying my code!

1 回表示 (過去 30 日間)
Alex
Alex 2014 年 12 月 23 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
if true
if navMemory.lastPos(1)>950
% targVel=-targVel;
% if navMemory.kk_temp >10
% navMemory.navState = 3;
% navMemory.kk_temp = 0;
% end
% elseif navMemory.lastPos(1)<-950
% targVel=-targVel;
% if navMemory.kk_temp >10
% navMemory.navState = 3;
% navMemory.kk_temp = 0;
% end
% elseif navMemory.lastPos(2)>950
% targVel=-targVel;
% if navMemory.kk_temp >10
% navMemory.navState = 3;
% navMemory.kk_temp = 0;
% end
% elseif navMemory.lastPos(2)<-950
% targVel=-targVel;
% if navMemory.kk_temp >10
% navMemory.navState = 3;
% navMemory.kk_temp = 0;
% end
% end end
Is there a way of sensibly shortening this? navMemory.lasPos is a variable with 2 values. I need these values to remain between 950 and -950, otherwise case 3 is called. This seems like a needlessly complicated way of doing it. (kk_temp is just a timer)
Many thanks as usual!

回答 (1 件)

Zoltán Csáti
Zoltán Csáti 2014 年 12 月 23 日
Since rows navMemory.navState = 3; and navMemory.kk_temp = 0; occur under the same condition (navMemory.kk_temp >10), you can put that condition outside. So
if navMemory.kk_temp >10
navMemory.navState = 3;
navMemory.kk_temp = 0;
end
if navMemory.lastPos(1)>950
targVel=-targVel;
elseif navMemory.lastPos(1)<-950
targVel=-targVel;
elseif navMemory.lastPos(2)>950
targVel=-targVel;
elseif navMemory.lastPos(2)<-950
targVel=-targVel;
end
  3 件のコメント
Alex
Alex 2014 年 12 月 23 日
Thank you - very good point! Is there any way of writing the following:
if -950<navMemory.lastPos>950
targVel=-targVel;
end
Thanks
Zoltán Csáti
Zoltán Csáti 2014 年 12 月 23 日
Yes, but you wrote you want navMemory.lastPos remain between -950 and 950. So
if navMemory.lastPos > -950 & navMemory.lastPos <950
...
end

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by