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!