matlab plot, interrupted line when y is zero

5 次查看(过去 30 天)
Hi,
I have a data that has some zero values but I would like to avoid to plot these values but I want a continues line between the previous and one after the data point. When I searched the web there are method to omit the zero values but the lines are broken. Instead I want to avoid the y=0 values but the code should plot a continues line before and after the y=0 values, Please see the plot. I would like to pass the data points of 5 and 7 and just connect the line between data point 4 and 6 and 6 and 7. Any idea? Thank you
figure(4)
x1=ModData(1:end,1);
x2=ExpData(1:end,1);
Mod=PD_Mod(:,7);
Exp=PD_Ex(:,4);
plot(x1,Mod,'gs-', x2,Exp,'r*-')

回答(1 个)

Star Strider
Star Strider 2019-8-23
Try this, using your own ‘x’ and ‘y’ vectors:
x = 0:10; % Create Data
y = randi([0 5], 1, 11); % Create Data
xnew = x;
ynew = y;
xnew(y == 0) = [];
ynew(y == 0) = [];
figure
plot(x, y)
hold on
plot(xnew, ynew)
hold off
grid
Experiment to get the result you want.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by