MATLAB: Long dashes in a dashed line matlab plot

linestyleslong dashesplotting

Good morning, I m plotting some results with matlab using the plot command as follows: plot(X,'color','r','linestyle','–','linewidth',1.5) I wonder if it s possible to change the length of the dashes to use longer ones !! Thank you in advance & Happy new year
Dima

Best Answer

For plotting on the screen, there is no straight solution. When you export the diagram to an EPS, there are some solutions in the FileExchange:
For the display on the screen you can use this workaround:
x = linspace(0, 4*pi, 400);
y = sin(x);
yDashed = y;
yDashed(10:20:length(yDashed)) = NaN;
plot(x, yDashed);
A nicer solution would be not to use equidistant steps on the X axis, but measure the curve length:
rangeX = max(x) - min(x);
rangeY = max(y) - min(y);
Q = [diff(x) / rangeX; diff(y) / rangeY];
L = [0, cumsum(sqrt(sum(Q .* Q)))];
L = (length(L) / L(end)) * L;
a = 5; % start point
b = 1; % gap width
c = 10; % gap frequency
index = rem(round(L) + a, c) <= b;
yDashed = y;
yDashed(index) = NaN;
plot(x, yDashed);