Infinite while loop in Matlab function block

조회 수: 5 (최근 30일)
Matthieu GATINE
Matthieu GATINE 2018년 10월 5일
답변: Walter Roberson 2018년 10월 8일
I have made this programm to implemente it in a Matlab function block in Simulink :
function Open_area1 = fcn(Moisture_Sensor_area_1_OUT,Moisture_instruction_min,Moisture_instruction_max)
Open_area1 = 0;
if (Moisture_Sensor_area_1_OUT<Moisture_instruction_min)
while(Moisture_Sensor_area_1_OUT<Moisture_instruction_max)
Open_area1 = 1;
end
end
end
But the while loop make an infinite simulation time.
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 10월 5일
That means the following condition is always true
Moisture_Sensor_area_1_OUT<Moisture_instruction_max
Do same sort of modification (update), so that it not true after some required iteration
Matthieu GATINE
Matthieu GATINE 2018년 10월 5일
편집: Matthieu GATINE 2018년 10월 5일
How can I update the value of Moisture_Sensor_area_1 when the function block is in the loop ? Does the simulation is stop during the execution of the function block ?
The input signal is a sampled sinus.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 10월 8일
Output = double(Input >= Min & Input <= Max) ;
No loop. No if. But you do need to substitute appropriate variables.

추가 답변 (1개)

TAB
TAB 2018년 10월 8일
I guess, you are trying to run the loop for checking Moisture_Sensor_area_1_OUT<Moisture_instruction_min condition for every sample time.
But Simulink's Matlab Function runs in different way. It is invoked at every sample time during model execution.
At t=0, Get the 3 function inputs from previous blocks, Call fcn with inputs, collect fcn output and send to next block
At t=1, Get the 3 function inputs from previous blocks, Call fcn with inputs, collect fcn output and send to next block
At t=2, Get the 3 function inputs from previous blocks, Call fcn with inputs, collect fcn output and send to next block
...
... and so on.
Inputs to fcn remains same during the a sample time, so loop will always run into infinity.
You don't need to use while loop. You can just use if condition to set output.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by