Creating while loop for control UNTIL condition is met

조회 수: 4 (최근 30일)
Jake Marquette
Jake Marquette 2019년 8월 2일
댓글: Jake Marquette 2019년 8월 2일
Hello -
I am in need of a while loop to act as a controller to keep a signal within a certain range. Basically I have a minimum and maximum value that I need an incoming signal to stay between cyclically. Here is the general process:
  • Increase value from 0 until the minimum is reached
  • One the minimum is reached, increase value until maximum is reached
  • Once maximum is reached, begin decreasing until minimum is reached again
  • Once minimum is reached, begin increasing till maximum. Repeat this cycle keeping the value between the min and max
This is a simplified version of my code so far
while true
if signal_value < min
% Increase from 0 until minimum
send increase.signal
elseif signal_value > min && signal_value < max
% Increase from minimum to maximum
send increase.signal
elseif app.ConvertedDataBuffer(end) >= app.highPressure
% Decrease once maximum is reached
send decrease.siganl
end
end
My code is getting caught up in the second elseif statement. Once it reaches the max and begins decreasing, it immediately begins increasing again. Is there a way I can decrease UNTIL the minimum is condition is met, and then increase again?
  댓글 수: 3
dpb
dpb 2019년 8월 2일
While Walter has the answer to the question, don't use min and max as variables (or constant stored in a variable) as they are Matlab builtin functions. Using them as you have above will alias the real function names making them unaccessible, not a good idea.
Jake Marquette
Jake Marquette 2019년 8월 2일
@Walter thank you, I was able to solve the issue using directional variables to further check.
@dpb, yes those aren't my real variable names I just used them to make the pseudocode easier to understand

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by