File Exchange

image thumbnail

Fix dashed and dotted lines in EPS export

version 1.0 (747 Bytes) by

EPS of dashed and dotted lines export badly. This modifies the EPS file to fix them.



No License

I was very frustrated at not being able to export dashed and dotted lines in EPS format, as they come out weird. Finally I found a post somewhere on the internet that suggested editing a certain part of the EPS file to fix it. This is a function which, given the name of the EPS file, will modify the length of the 'dot', making it look better in the image.

I've found it useful to automatically add this function to my standard 'save figure' script, so it is always run.

In EPS file:
/DO { [.5 dpi2point mul 4 dpi2point mul] 0 setdash } bdef

It seems the optimal numbers to go in the EPS file depend on the thickness of the line being drawn. This code changes only the .5 which corresponds to the length of the 'dot'. The number 4 controls the space between the dots. Setting both equal to 1 seems to work well with a Matlab line thickness of 1.5.

Comments and Ratings (18)

I also use Matlab R2016b in Linux. I face the similar issue as mentioned by user "yingwee" below. I guess there is some change in eps file writing syntax and therefore the new eps file is written with meaningless text.


I tried script this using Matlab 2016b, Windows 10, 64 bit, but the .eps file generated using this version of Matlab no longer has /DO, /DD in the file, hence this script does not work. The new way dashed lines are defined in the .eps files are "[8 8] 0 setdash" etc.

Works right away in Matlab 2016b!

Xinfu Liu

Thanks! It makes the outputs look much much better. I found that the grid lines are also lengthened. Is is possible to make any change in the code so that the grid lines are intact?

Steve Eddins

Steve Eddins (view profile)

If you are using this submission, you might want to see if you think it is still needed with MATLAB R2014b or later. EPS output for dashed and dotted lines has been improved in those releases.


Jeremy (view profile)

Thanks! Similar to other FEX fixes I've tried, but I like the spacing between dashes and dots with this fix.

Thank you so much!
Still I don't understand why such bugs are not corrected by Matlab right away. Using Matlab for publications is always horrible!


Natan (view profile)

works great thanks!

hi, does anyone know why I can't see dashed or dotted lines in matlab figures when using plot3?


Cesare (view profile)


Oliver Woodford

See fix_lines ( for an equivalent function which automatically changes dash length according to line width.

Oliver Woodford

This is a great idea. I haven't downloaded the file because I found the functionality first in fixPSlinestyle. However, you gave me the idea of automatically changing dash length with line width. This functionality is now available in my print_pdf and print_eps functions, so manual tinkering on a figure by figure basis is no longer needed.

Klaas van 't Veld

Outstanding fix of a very annoying problem. I modified the script a bit to make it more flexible, adding arguments "dotlen" and "dotgap"

function fix_dottedline(filename,dotlen,gaplen)

adding these lines at the top

dotstr = ['[' num2str(dotlen)];
gapstr = ['mul ' num2str(gaplen)];

and changing the replacement operations (for both the DO and DD lines) to

thisLine = regexprep(thisLine,'[.5',dotstr);
thisLine = regexprep(thisLine,'mul 4',gapstr);

That way you can play around with dot lengths and gap lengths until everything is just perfect.

Rich Germuska

Nice one.

For those still using legacy versions e.g. 6.0 release 12, just replace strfind() with findstr() as appropriate.

Works nicely on WinXP Pro.

Ronco Badsmith

This is a great fix. Thanks for the upload!

MATLAB Release
MATLAB 7.4 (R2007a)

Inspired: export_fig

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video