# how to implement while loop with time condition in matlab?

I knew how to set a a while loop in Mikrocontroller, but when I tried to Implement it in matlab /simulink, my computer gets stuck and never get out of the while loop and does not do anything. here is my code:

function [tout,valueP,valueI] = fcn(tin,Vout)

persistent dp;          %local variable
persistent di;          %local variable

if isempty(di)
di=0;
end
if isempty(dp)
dp=0;
end

while 1                 % infinite loop

if tin==30          %at time t=30s, take Vout value
if Vout==300    %during this time, compare Vout to 300V
di=10;
dp=40;
else
di=20;
dp=10;
end
end

if tin==60            % break the loop at time 60s
break
end
end

tout=tin;               % output
valueP=dp;
valueI=di;


my objective is: run the model, then watch the Vout and then at time 30s, take the value of Vout, then use it for comparaison that will result in vlaueP,valueI. here is my model as pic(could not add it)

• Yes, you are right – Yaakov Sep 24 '20 at 7:29
• Use a fixed timestep for the simulation. – Srinivasan M P Sep 24 '20 at 11:40
• What t variable in the line t==60 ? where is it defined ? What changes its value ? Your code ? – AJN Sep 24 '20 at 13:28
• Why is there a loop in a code that will be repeatedly called by simulink. The loop is already written inside Simulink. Your code probably doesn't need its own loop. Just remove the loop and try again. – AJN Sep 24 '20 at 13:30
• Alternately try changing the line t==60 to t>=60. That way, if Simulink skips over exact 60s, the loop will still break. – AJN Sep 24 '20 at 13:33

tin is an argument to your function, as a result, it cannot change value during a call to the function, and the condition for loop termination cannot be satisfied, unless it was from the very beginning.

You are going to have rethink the whole design of your program such that it can actually become aware of changes in the value of whatever tin represents.

Probably you want the while loop to wrap whatever obtains the value, if you then still use a function at all, you would call it once per iteration of the while loop (it's possible that however this is hooked up in your software package would already effectively do that by continuously re-executing your function - ie, the mistake may be having your own while loop at all)

The same concern would go for your other input, Vout as well.

I'd suggest you take a step back and find a simpler example of a function that plugs into this sort of simulation system, and take time to understand how it operates on time-varying inputs. Once you really understand how that works, you can re-implement the logic you need in a way compatible with this framework.

• tin represent time, I wanted this function be aware of the time – Yaakov Sep 24 '20 at 14:44
• Obviously you want it to respond to change over time, but the way you've written this it can only act on a single value of tin and Vout. You need to find yourself a simple, generic example of how to make a system which responds to a time varying input in this software, take time to understand how it works, and only then adapt it into something implementing the behavior you need. – Chris Stratton Sep 24 '20 at 14:46
• now I tried it with if and only for using greater or smaller than it works. I did not understand what you mean by 'Probably you want the while loop to wrap whatever obtains the value' – Yaakov Sep 24 '20 at 14:50
• If your function and your diagram are the entirety of your "program" then I suspect the simulation framework is already providing the "while loop to wrap whatever obtains the value" meaning you yourself do not need a while loop. – Chris Stratton Sep 24 '20 at 14:52
• actually this is just a small part of the program that I wanted to implement , my objective was to have di and dp changing according to the voltage Vout when my program reach 3s. – Yaakov Sep 24 '20 at 16:23