Why wont the lines on my plot show up

1 view (last 30 days)
Sameeha Meher
Sameeha Meher on 4 Mar 2021
Answered: Cris LaPierre on 4 Mar 2021
function phaseLine(f, ymin, ymax)
y0=[ymin:0.1:ymax];
a=y0(y0==0);
a=unique(a);
b=y0(y0~=0);
n=length(a)+length(b);
figure
if b>0
t=linspace(-1,1,n);
plot (t,b,'-b')
else if b<0
t=linspace(-1,1,n);
plot (t,b,'-r')
else
t=linspace(-1,1,n);
plot (t,a,'-k')
end
end
it doesnt work if i write linspace before the figure either

Answers (1)

Cris LaPierre
Cris LaPierre on 4 Mar 2021
I think the issue is that you are using a vector in the conditional statement of your if statement. An if statement expects a single logical result. Your vector likely contains some values that are true for each condition and some that are false. Since they are not all true, only the else statement runs.
For your else case, you plot t and a. The issue here is that a is a single value, 0 (assuming your range allows for this). Since t is a vector, it will create a new series for each combination of t with a. Since there is only a single data point in each series, no line appears. Because you did not specify a marker in your lineSpec, nothing appears.
Perhaps you meant to do something like this.
y0=-5:0.1:5;
t=linspace(-1,1,length(y0));
figure
plot (t(y0>0),y0(y0>0),'-b')
hold on
plot (t(y0<0),y0(y0<0),'-r')
plot (t(y0==0),y0(y0==0),'-^k')
hold off

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!