Everytime I run code the color of lines on my graph changes

조회 수: 11 (최근 30일)
Vinay
Vinay 2024년 10월 2일
댓글: Vinay 2024년 10월 2일
load ENGR131_lab4_Data.mat
function h = CalcPosition(t,DC,w)
wn=1.8
o=DC.*wn;
h=1-exp(-o.*t).*cos(w.*t);
end
function x = five(t,N,V,h,w)
z=evalin('base','DC')
for N=1:2
switch N
case 1
plot(t,h)
hold on
case 2
plot(t,h);
hold on
case 3
LineColor='k-';
plot(t,h,':');
hold on
case 4
LineColor= 'm-';
plot(t,h,'-.');
hold on
end
end
end
for V=1
subplot(2,3,V)
t=linspace(0,20,100)
for N=1:2
figure(1)
h=CalcPosition(t,DC(1,N),w(1,V))
five(t,N,V,h,w)
end
end

채택된 답변

Rik
Rik 2024년 10월 2일
편집: Rik 2024년 10월 2일
That makes sense, since you don't actually set the LineColor property when calling plot (instead you just have a variable with that name).
This means that every time you call this function, the same figure is being reused and a new line is plotted overlapping the existing lines, making it look as if the line colors are changing.
The solution is to do what you intended to do:
LineStyle='-.'
LineColor='m';
plot(t,h,LineStyle,'LineColor',LineColor)
You should also consider using explicit handles: create a new figure (or wipe the old one with clf) and a new axes, use hold on that axes object, plot, and release the hold.
And your use of evalin signals that you should rethink how you handle input arguments.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by