Function ...
...
if s<200
logic=1;
else
logic=0;
end
end
end
here I'll explain what I tryna make, assume 's' is a variable result of that firstly starting from 0 and having cumulative summation step by step. Till it arrives at 200, logic equals to 1, so far so good, where it arrives at 200, I want the logic be constant 0 always after at 200, the problem is 's' is decreasing after it got hit by 200. So that it starts to be '1' again. That's the thing I don't want it to be. If it happened in a loop, I'd use 'break', but loops is not used.
For any help, I thank you for your time in advance.

 채택된 답변

Stephen23
Stephen23 2019년 7월 23일
편집: Stephen23 2019년 7월 24일

0 개 추천

Use a nested function:
logic = true;
...
function ...
...
logic = logic && s<200;
...
end
...
Or a persistent variable:

댓글 수: 5

Enez Furkan Cihan
Enez Furkan Cihan 2019년 7월 23일
It makes no difference, for example, s arrived at 200, logic became 0, but whenever code goes to the beginning of the function and s decreases below 200, logic will be 1 again.
Stephen23
Stephen23 2019년 7월 23일
@Enez Furkan Cihan: your explanations are not very clear (e.g. what does " whenever code goes to the beginning of the function" actually mean?), but apparently you are calling this function multiple times and expect the logic value to depend on its value in previous function calls. In that case, you need to store the value using persistent or use a nested function. My answer shows how to do this using a nested function.
Image Analyst
Image Analyst 2019년 7월 23일
Yes, how can it go back to the beginning of the function when you said " but loops is not used."???
Can you post a fully working simplified version of your program?
Enez Furkan Cihan
Enez Furkan Cihan 2019년 7월 23일
@Stephen Cobeldick , I did solved the problem using persistent. For other people possible may have the same problem I had, I forgot to mention the code is in MATLAB Function in Simulink, and function is called multiple times naturally. Any way, It’s done.
Stephen23
Stephen23 2019년 7월 24일
@Enez Furkan Cihan: I am glad that my suggestion of using peristent helped you. You can help me by voting for my answer or accepting it.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 7월 23일

0 개 추천

Instead of this
if s<200
logic=1;
else
logic=0;
do this
logic = s < 200; % Just one line, not a whole if/else block.
if logic
return; % Exit out of the function entirely so s does not get altered anymore.
end

댓글 수: 1

Enez Furkan Cihan
Enez Furkan Cihan 2019년 7월 23일
Function is not only set up for this logic part. I can't go out of the function before it completes its sampling time.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2019년 7월 23일

편집:

2019년 7월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by