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 Comments

Image Analyst
Image Analyst on 3 Jan 2019
Edited: Image Analyst on 3 Jan 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
Burak Akyol on 8 Jan 2019
Edited: Burak Akyol on 8 Jan 2019
Hi there, thanks for your reply and sorry for my late response.
I have an initial signal which gives only 1 or 0.
This signal should be given as input to my function.
->In my function, while input signal is 1;
there should be a sine wave function run from 0 to pi/2 with 0.01 step
and sine function results should be my function output
->when signal turns out to 0
my function output should be 7500.
Would that make my issue a bit clear?
Thanks

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 3 Jan 2019
Edited: Cris LaPierre on 3 Jan 2019
Check out this answer. It might help you out.
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 Comments

Hi Cris, thanks for your reply and sorry for my late response.
I think, your link is not directly linked to my issue. I posted a detailed explanation of my issue to above comment. I'm still trying to configure w/o success.
Thanks.
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
Burak Akyol on 8 Jan 2019
Edited: Burak Akyol on 8 Jan 2019
Dear Cris,
I appreciate your answer and your advise on MATLAB Onramp. I will definitely go over the videos.
Actually, the reason that I was trying to prefer 'while' was; Simulink. I have a model in Simulink which I'm trying to implement this algorithm into. Initially I had used 'If' block, it was like; if condition is applied, run the sine wave, if doesn't; link to a constant which is 7500 (it seemed a way to me). However when I saw the scope, I realized that sine wave had started and ended with simulation time and gave sine wave output which corresponded to those intervals.
It was like I ran a sine wave during my simulation time and focused on results at specific time intervals.
I only initiate a sine wave at a time interval that my condition is applied. Do you have a solution in Simulink blocks?
That's why I assigned a 'fcn' block to Simulink and started to do something in editor.
Sorry for bothering you for multiple times and thank you for your all help.

Sign in to comment.

Products

Release

R2018a

Asked:

on 3 Jan 2019

Edited:

on 8 Jan 2019

Community Treasure Hunt

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

Start Hunting!