While Loop if Condition is True
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 3 Gen 2019
Modificato: Image Analyst
il 3 Gen 2019
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
il 8 Gen 2019
Modificato: Burak Akyol
il 8 Gen 2019
Risposte (1)
Cris LaPierre
il 3 Gen 2019
Modificato: Cris LaPierre
il 3 Gen 2019
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 Commenti
Burak Akyol
il 8 Gen 2019
Cris LaPierre
il 8 Gen 2019
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
il 8 Gen 2019
Modificato: Burak Akyol
il 8 Gen 2019
Categorie
Scopri di più su Sources in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!