Simulink stuck on while loop with Time condition
조회 수: 3 (최근 30일)
이전 댓글 표시
I want to implement a while loop inside a MATLAB Function in Simulink, whose condition is dependant on time and would not change otherwise. But when I try to run the simulation, it shows "Running" but does not progress beyond that. My real function is rather complex, but here is an example of what I wanted to do:
function [Discharge,Charge] = Decode(Matrix,Time)
mat = Matrix(Matrix~=0);
mat = reshape(mat,numel(mat)/3,3);
Discharge = zeros;
Charge = zeros;
while Time <= 60
Discharge = mat(i,1);
Charge = mat(i,2);
end
end
댓글 수: 0
채택된 답변
Image Analyst
2022년 2월 13일
Look at your while loop. If it gets in there, how does it ever leave? Time does not change in the loop so if Time is less than 60 it will get stuck in an infiinte loop. Another problem with it : since "i" never changed, the Discharge and Charge value will never change. Third problem : there is no failsafe - a way to quit the loop if you exceed some number of iterations that you believe should never be reached. Possible fix:
maxIterations = 1000;
[rows, columns] = size(mat);
loopCounter = 1;
startTime = tic;
elapsedSeconds = toc(startTime);
while (elapsedSeconds <= 60) && loopCounter < maxIterations
Discharge = mat(loopCounter, 1);
Charge = mat(loopCounter, 2);
loopCounter = loopCounter + 1;
elapsedSeconds = toc(startTime);
end
You might also want to do some checks on Charge and Discharge since those are the things that are changing in the loop.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Electrical Sensors에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!