Implementing a while loop
1 view (last 30 days)
Show older comments
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 Comment
Adam
on 9 Mar 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?
Answers (1)
Brandon Eidson
on 15 Mar 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).
1) use a basic Stateflow chart: https://www.mathworks.com/help/stateflow/programming-in-stateflow.html
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.
0 Comments
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!