While Loop if Condition is True
이전 댓글 표시
Hello Everyone,
I'm just confused about how to set a 'while loop' that works only when my condition is true. I need to create a 'quarter sine signal' while a condition (a derivative input) is true.
Actually, I'm trying to create a signal on Simulink, but it looks like it can't be handled w/o using editor functions.
Basically, I need to build a signal which gives 7500 if condition is false. If condition is true, there should be a sine function initiated and lasts until condition gets false.
In Simulink, I link my condition via 'If' block and send it to 'Signal Builder' block, however in order to create sine wave, I put Sine Wave block and feed the builder with it by using another if block. Problem is; Sine Wave function starts with simulation time and when my condition is true, it doesn't start from zero, it only gives the sine input at that simulation time.
In Matlab editor, it tried below w/o success;
function (y)
while y>0
a=0:0.01:pi/4
y=sin(a)
else
y=7500
end
end
Sorry for this long query, but can someone help me on solution for this?
Thank you in advance.
댓글 수: 2
Image Analyst
2019년 1월 3일
편집: Image Analyst
2019년 1월 3일
But your y is a vector (many, many values) because "a" is a vector. y is not a single true or false value like "while" expects. Under what condition do you want to break out of the while loop?
Burak Akyol
2019년 1월 8일
편집: Burak Akyol
2019년 1월 8일
답변 (1개)
Cris LaPierre
2019년 1월 3일
편집: Cris LaPierre
2019년 1월 3일
Also, you need to give your function a name and a variable to output:
function out=myFunc(y)
What are typical inputs for y?
댓글 수: 3
Burak Akyol
2019년 1월 8일
Cris LaPierre
2019년 1월 8일
No, it was mostly there to show you an example of a while loop.
If you are really new to programming in MATLAB, I'd strongly recommend you invest a couple hours to go through MATLAB Onramp. Even if you don't do it all, look at the chapters and go through those that seem applicable to what you need right now.
I don't think you need a while loop. You need an if statement
y =1;
y = myFunc(y)
function out = myFunc(y)
if y>0
a=0:0.01:pi/4;
out=sin(a);
else
out=7500;
end
end
Burak Akyol
2019년 1월 8일
편집: Burak Akyol
2019년 1월 8일
카테고리
도움말 센터 및 File Exchange에서 Sources에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!