how to make if statement repeat only once ?

조회 수: 39 (최근 30일)
tomer polsky
tomer polsky 2018년 5월 16일
댓글: Mohamed Eldemerdash 2021년 11월 2일
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
Stephen23
Stephen23 2018년 5월 17일
편집: Stephen23 2018년 5월 17일
tomer polsky's "Answer" moved here:
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
Guillaume 2018년 5월 22일
Since the if statement is outside of any loop, it will execute only once. Hence it's not clear what the problem is.

댓글을 달려면 로그인하십시오.

답변 (1개)

Aakash Deep
Aakash Deep 2018년 5월 22일
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

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by