Implementing a while loop

1 Ansicht (letzte 30 Tage)
BEngEEStudent
BEngEEStudent am 9 Mär. 2017
Beantwortet: Brandon Eidson am 15 Mär. 2017
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 Kommentar
Adam
Adam am 9 Mär. 2017
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Brandon Eidson
Brandon Eidson am 15 Mär. 2017
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