Variable change should not affect during loop execution?

조회 수: 4 (최근 30일)
Sanjeet Kulkarni
Sanjeet Kulkarni 2021년 11월 12일
댓글: Sanjeet Kulkarni 2021년 11월 30일
How should I not update the variable if its value is changed from dashboard during execution? For example:
If a variable value is 1 then throughout the time of execution of code it should be 1. If its value is changed from dashboard to 2 in between, then the loop should first complete its execution using the previous value and once it has completed then only the variable value should be 2 and then rest of the execution should be done. Basically, what I am trying to do is, changing variable value in between should not affect the loop, only after it has completed its execution, then it should take the new value. Can anyone guide me on how should I do it?
  댓글 수: 3
Sanjeet Kulkarni
Sanjeet Kulkarni 2021년 11월 30일
Using the assignin function?
Sanjeet Kulkarni
Sanjeet Kulkarni 2021년 11월 30일
Eg: Two variables a and b in simulink. Simulink calls one matlab function while executing.
Function
if (condition)
output = b;
else (condition)
output= a;
end
If value is changed of a and b, the change should be reflected after it has come out of the if loop. What other condition can I put in with if loop or any other way by which it can be done?

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

답변 (1개)

Walter Roberson
Walter Roberson 2021년 11월 30일
Function
for variable = 1 : number_of_iterations
original_a = a;
original_b = b;
if (condition)
output = original_b;
else (condition)
output= original_a;
end
end
If a or b are changed through the dashboard any time between copying them to the original_* variables and the end of the loop iteration, then the old version wil be used until the next loop iteration.
If it is important that the user should be able to change the value with the dashboard between the time of the "for variable" loop moving to the next iteration, and the point where this code copies the variables, then a different approach would need to be taken.
  댓글 수: 1
Sanjeet Kulkarni
Sanjeet Kulkarni 2021년 11월 30일
I feel a different approach should be taken.
for variable = 1 : 100
original_a = a;
original_b = b;
if variable < 50
output = original_b;
else
output= original_a;
end
end
Using this, if a=1, b=1 and if I change a and b values before variable reaches 100 the output reflects this change.
What I am trying to do is, if values of a&b are changed to 2 before variable reaches 100, the for loop should execute completely and when variable starts again from 1 after reaching 100, that time the output should reflect the changes and it should change from 1 to 2.

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

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by