필터 지우기
필터 지우기

Implementing a while loop

조회 수: 3 (최근 30일)
BEngEEStudent
BEngEEStudent 2017년 3월 9일
답변: Brandon Eidson 2017년 3월 15일
I want to create a while loop which loops until the end of simulation. Is this possible? My matlab function block has 2 inputs (idir , and batsoc) and 2 outputs (goboost & gobuck)
my code is:
function [gobuck, goboost]= fcn(batsoc, idir)
gobuck = 0;
goboost = 0;
while (idir < 0) || (idir > 0)
gobuck = 0;
goboost = 0;
if batsoc < 97
gobuck = 1;
goboost = 0;
elseif batsoc > 98
goboost = 1;
gobuck = 0;
end
end
end
end
Thanks
  댓글 수: 1
Adam
Adam 2017년 3월 9일
idir never changes in your while loop so it isn't an appropriate loop constraint.
What is it that is supposed to signal the end of your simulation in order for the loop to stop?

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

답변 (1개)

Brandon Eidson
Brandon Eidson 2017년 3월 15일
Hey Fazal, a MATLAB Function block with execute each simulation time step. This means that, each time step, the entire while loop will be executed. Based on your post and the fact that "idir" is not updated in the MATLAB Function block, I do not think this is what you intend.
Because your outputs look like booleans and are based on some simple logical conditions, I think you have a couple good options for implementing this outside of a MATLAB Function block (and outside of a while loop).
2) use a combination of Logic Operator blocks: https://www.mathworks.com/help/simulink/logic-and-bit-operations.html
You may already be utilizing these but, in case not, you should be aware of various conditionally-executed subsystems.
Combining these features should allow you to implement your desired model.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by