# How to plot curves with different colors with "if" conditions

11 views (last 30 days)
EldaEbrithil on 18 May 2021
Commented: EldaEbrithil on 18 May 2021
Hi all
i want to realize a multilines plot using if conditions. The target is to plot each row of r_T for different columns(or "Weight" values). i_ed can ony varies between 1 and 5 so i decide to differentiate the colors of the curves. i_ed,j_ed,k_ed,w_ed,v_ed have the same dimension. How can i do that?
Thank you for the help!!
for i=1:length(i_ed)
for w=1:length(Delta_cb)
r_T(i,w)=0.5*rho*(Larg_cb(j_ed(i))*T_end(k_ed(i))+2*(L_cb(i_ed(i))*(Larg_cb(j_ed(i))/(2*cosd(((atan(2*T_end(k_ed(i))/Larg_cb(j_ed(i))))*180/pi)))))*2)*...
((1+(-0.095+25.6*(((Weight(w)/(rho))/(L_cb(i_ed(i))*Larg_cb(j_ed(i))*T_end(k_ed(i))))/...
(((L_cb(i_ed(i))/Larg_cb(j_ed(i)))^2)*sqrt(Larg_cb(j_ed(i))/T_end(k_ed(i)))))))*(0.075/...
((log(((rho*V(v_ed(i))*L_cb(i_ed(i)))/(mu_din)))-2)^2))+(1.85e+04*((V(v_ed(i))/(sqrt(9.81*L_cb(i_ed(i)))))^6) -...
1.92e+04*((V(v_ed(i))/(sqrt(9.81*L_cb(i_ed(i))))))^5 + ...
7.38e+03*((V(v_ed(i))/(sqrt(9.81*L_cb(i_ed(i))))))^4 - 1.30e+03*...
((V(v_ed(i))/(sqrt(9.81*L_cb(i_ed(i))))))^3 + 1.06e+02*((V(v_ed(i))/...
(sqrt(9.81*L_cb(i_ed(i))))))^2 - 2.65e+00*(V(v_ed(i))/(sqrt(9.81*L_cb(i_ed(i)))))...
+ 7.96e-02))*V(v_ed(i))^2;%N
end
end
for i=1:length(i_ed)
if i_ed(i)==1
c = 'r';
if i_ed(i)==2
c = 'b';
if i_ed(i)==3
c = 'y';
if i_ed(i)==4
c = 'g';
if i_ed(i)==5
c = 'k';
figure(1);
set(figure(1),'Visible','on');
plot(Weight(:),r_T(i,:), c,'LineWidth',1);
xlabel('Weight_K_g')
ylabel('RT_N')
hold on
end
end
end
end
end
end

Rik on 18 May 2021
You have nested your if statements, which would have been very easy to spot if you had used the debugger to step through the code.
You can replace the plotting loop with this:
f=figure(1);
set(f,'Visible','on');
c_array={'r','b','y','g','k'};
for i=1:numel(i_ed)
c=c_array{i_ed(i)};
plot(Weight(:),r_T(i,:), c,'LineWidth',1);
hold on
end
xlabel('Weight_K_g')
ylabel('RT_N')
hold off
EldaEbrithil on 18 May 2021
Thank you very much!!

### Categories

Find more on Line Plots in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by