how to make if statement repeat only once ?
Show older comments
hello I am currently doing a project with matlab and simulink.
In my project I am taking current samples from matlab function and making is statment for a switch .
the problem is that my statment is working all the time , meaning that for exmaple my switch is on "1" and it is switching to "0" I want it to stay like this and not creak the statement again how is it possible ?
function [final_resolt]=relay_condition(u)
format long;
i_diff_1=u(1)-u(4);
i_diff_2=u(2)-u(5);
i_diff_3=u(3)-u(6);
ind_1=u(1)*u(4);
ind_2=u(2)*u(5);
ind_3=u(3)*u(6);
I_diff_1=0;
I_diff_2=0;
I_diff_3=0;
IND_1=1;
IND_2=1;
IND_3=1;
m=20;
I_min=1000;
for i=1:1:m
I_diff_1=(I_diff_1+i_diff_1^2);
I_diff_2=(I_diff_2+i_diff_2^2);
I_diff_3=(I_diff_3+i_diff_3^2);
IND_1=(IND_1*ind_1)/1;
IND_2=(IND_2*ind_2)/1;
IND_3=(IND_3*ind_3)/1;
end
I_diff_1=(I_diff_1/m)^0.5;
I_diff_2=(I_diff_2/m)^0.5;
I_diff_3=(I_diff_3/m)^0.5;
IND_1=IND_1/m;
IND_2=IND_2/m;
IND_3=IND_3/m;
if (I_diff_3>I_min )
final_resolt=0;
else
final_resolt=1;
end
end
3 Comments
Geoff Hayes
on 17 May 2018
tomer - please clarify what you are attempting to do as it isn't clear (to me) what the switch is. Is the problem with the if block? Do you only want to evaluate it once?
Ye this exactly what I want to do . u are currents from simulink and the final resolt is the switch . I want it to change only when time , when it changes to zero stay in this condition.
Guillaume
on 22 May 2018
Since the if statement is outside of any loop, it will execute only once. Hence it's not clear what the problem is.
Answers (1)
Aakash Deep
on 22 May 2018
Hey,
From your question, I understand that you have a if condition inside a loop which you need to execute only once for a particular condition. In order to do this you can use a flag variable. Let's see this with an example, let's suppose you have an array with values 0 and 1. You have to subtract value x only on the first zero in the array.
arr = [1,1,1,0,0,1,0,0];
flag = 0;
for i = 1:len(arr)
if( flag == 0 && arr(i) == 0 )
arr(i)= arr(i) – x;
flag = 1; % important part
end
% loop_continue
end
Hope this helps
Thanks,
Aakash Deep
1 Comment
Mohamed Eldemerdash
on 2 Nov 2021
Thanks Akash this worked for me.
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!