if statements inside for loop issue

1 次查看(过去 30 天)
My code is suppose to compare state of charge of batteries (SOC1 SOC2 SOC3 - - -). based on that, give values to the output arguments (P1,P2,P3 - - - ). the values to be given are inside the "Pulses" variable. "Pulses" is variable containing 12 pulse signal with different duty cycles, Pulses (:,1) being the signal with the biggest duity cycle. Now what I am trying to do is if SOC1 is the highest give P1 the pulse with the biggest Duty cycle which is Pulses (:,1). if SOC1 is the second highest give P1 Pulses(:,2) which the signal with the second biggest duty cycle. the same for all Ps. this is in MATLAB Function block in Simulink. the code below is how I tried it. but I only get value for P12. I kindoff understand why it's happening but don't know how to fix it. please also suggest if you think there is better way to do it. if needed i can provide deep insight of the system.
function [P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12] = fcn(Pulses,SOC1,SOC2,SOC3,SOC4,SOC5,SOC6,SOC7,SOC8,SOC9,SOC10,SOC11,SOC12)
K1=SOC1*100000; K2=SOC2*100000; K3=SOC3*100000; K4=SOC4*100000; K5=SOC5*100000; K6=SOC6*100000;
K7=SOC7*100000; K8=SOC8*100000; K9=SOC9*100000; K10=SOC10*100000; K11=SOC11*100000; K12=SOC12*100000;
SOC = [K1 K2 K3 K4 K5 K6 K7 K8 K9 K10 K11 K12];
[A,B]= sort (SOC,'descend');
for i = 1:12
if 1 == B(1,i)
P1 = Pulses(:,i);
else
P1=0;
end
if 2 == B(1,i)
P2 = Pulses(:,i);
else
P2=0;
end
if 3 == B(1,i)
P3 = Pulses(:,i);
else
P3=0;
end
if 4 == B(1,i)
P4 = Pulses(:,i);
else
P4=0;
end
if 5 == B(1,i)
P5 = Pulses(:,i);
else
P5=0;
end
if 6 == B(1,i)
P6 = Pulses(:,i);
else
P6=0;
end
if 7 == B(1,i)
P7 = Pulses(:,i);
else
P7=0;
end
if 8 == B(1,i)
P8 = Pulses(:,i);
else
P8=0;
end
if 9 == B(1,i)
P9 = Pulses(:,i);
else
P9=0;
end
if 10 == B(1,i)
P10 = Pulses(:,i);
else
P10=0;
end
if 11 == B(1,i)
P11 = Pulses(:,i);
else
P11=0;
end
if 12 == B(1,i)
P12 = Pulses(:,i);
else
P12=0;
end
end

采纳的回答

dpb
dpb 2022-5-9
编辑:dpb 2022-5-9
"I know nuthink!" about Simulink, but the problem you have in the function is primarily that you've created all those named variables instead of using arrays and the power of MATLAB syntax --
function P=fcnPulse(Pulses,SOC)
% assign output pulse array P from Pulses duty cycle in descending order
% of strength of input batteries array, SOC
[~,ix]=sort(SOC,'descend');
P(ix,:)=Pulses;
end
Now, how you get to/from the Simulink model individual batteries/pulses to the MATLAB arrays I don't know, but there must be a facility with which to do so.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 System on Chip (SoC) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by