Implementing a while loop

1 次查看(过去 30 天)
BEngEEStudent
BEngEEStudent 2017-3-9
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.

Community Treasure Hunt

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

Start Hunting!

Translated by