Implementing a while loop

1 回表示 (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by