Saving MATLAB graphs in a specific resolution - matlab

How do I change the resolution of my MATLAB graphs? The default seems to be 96x96 PPI.

You can save using the print command; here's how:
h1=figure;
your plotting commands here
print(h1,'-djpeg','-r150','filename')
The -r150 argument sets the resolution to 150 DPI. you can set it higher or lower depending on your need. This is assuming you're saving as a JPEG. If not, there are other options such as -dpng for PNG, -dbmp for 24-bit bitmap, etc. more can be found in the help section for print.
That said, if your plot consists only of lines, I would recommend using -depsc which saves it as an EPS file, which is scalable and lossless. You also won't have to define a resolution, so you can simply do
print(h1,'-depsc','filename')
You can convert this EPS file to PDF and retain the same resolution, yet have portability between applications and platforms. I wouldn't recommend saving to PDF directly as MATLAB puts additional white spaces around the plot, wheras the EPS bounding box is tight, and looks neat.

Changing a Figure's Settings: Setting the resolution

Related

Change size of figure in MATLAB to use them in LaTeX

I have generated several plots in MATLAB and I haven't set the ( width or height) to any of them. I save them as pdf to place them in LaTeX but each figure appear with different size. I can change each figure size in LaTeX but I wonder if there is an easy way in MATLAB to get same size for all figures.
Can I reset the figure size after generating in MATLAB or I have to regenerate each figure with specific size?
What is the best way to export figures in MATLAB to use them in LaTeX?
Exporting figures from MATLAB to LaTeX is best done via .eps or directly to PDF. MATLAB's saveas() functionality supports both. The benefit of these file-types is that thy are vector files, meaning, loosely, that coordinates of corners in the lines are saved, as opposed to a bunch of fixed pixels such as with PNG or JPG figures. Using then these figures in LaTeX gives infinite scalability (theoretically, usefulness of highly-zoomed figures may vary).
If you need different aspect ratios, i.e. one figure taller than broad, the other broader than tall, You'd have to set the proper aspect ratio for each figure independently, then save using one of the aforementioned vector file types.
One of the best ways to export pretty figures is the export_fig() functionality written by Oliver Woodford and currently maintained by Yair Altman and freely available on the FileExchange.
Answering your first question: yes you can resize figures after generating them. Either by hand (change the window size), or programatically: you can make a figure handle (h=figure();, where h is your handle), which has several options to control figure size; h.position being the most prominent: [upper-left, lower-left, upper-right, lower-right] being its content in terms of corner coordinates. Changing those changes the figure size.

Using GhostScript to export PNGs at fixed size

We have many square EPS images, which we would like to export via script to PNG at very specific formats/sizes, namely
8192x8192, greyscale, no alpha, no anti-aliasing
2048x2048,greyscale, no alpha, anti-aliased.
We have had no luck scripting the "professional" tools Photoshop or Illustrator to do this (although we can do so through the UI, their weak scripting support does not give control over alpha or precise image export size, so we either always get alpha in the large images, or we sometimes get slightly inaccurate image sizes which breaks subsequent algorithms.)
Our first attempt at doing the high resolution version of this was:
gs -sDEVICE=pnggray -o cover.png -dDEVICEWIDTHPOINTS=8192 -dDEVICEHEIGHTPOINTS=8192 -dGraphicsAlphaBits=1 -dPDFFitPage=true cover.eps
However, this does not seem to resize the image to fill the box as expected.
Is there a way, given a square EPS, to get Ghostscript to do what we want?
Your problem with EPS files is that they do not request a media size. That's because EPS files are intended to be included in other PostScript programs, so they need to be resized by the application generating the PostScript.
To that end, EPS files include comments (which are ignored by PostScript interpreters) which define the BoundingBox of the EPS. An application which places EPS can quickly scan the EPS to find this information, then it sets the CTM appropriately in the final PostScript program it is creating and inserts the content of the EPS.
The FitPage switch in Ghostscript relies on having a known media size (and you should set -dFIXEDMEDIA when using this) and a requested media size, figuring out what scale factor to apply to the request in order to make it fit the actual size, and setting up the CTM to apply that scaling.
If you don't ever get a media size request (which you won't with an EPS) then no scaling will take place.
Now Ghostscript does have a different switch, EPSCrop which picks up the comments from the EPS and uses that to set the media size (Ghostscript has mechanisms to permit processing of comments for this reason, amongst others). You could implement a similar mechanism to pick up the BoundingBox comments, and scale the EPS so that it fits a desired target media size.
I could probably knock something up, but I'd have to mess around creating an example file to work from.....
Do not accidentally specify PDFFitPage in the command line above. Specify EPSFitPage when dealing with EPS files. PDFFitPage will silently do nothing.

exporting figure to eps file

I am trying to export a figure which includes highlighting of regions - something like this: Highlight parts of matlab plot. Unfortunately, when I export the figures to .eps files the size is of the order of ~10 MB... Thus, when I include them in a tex file, the quality is severely degraded. As expected this problem seems to occur due to the use of the area function for the highlighting. Is there any workaround on this?
You have transparency in your plot which requires the renderer to be OpenGL which causes any EPS to not really be vector graphics but rather bitmaps coerced in some strange way. This is why the file size is much larger than what you would expect for vector graphics. If you open the resulting EPS file with an external editor (Illustrator, Inkscape, etc.) you will see what I am talking about. As a side-note, transparency isn't technically supported in EPS files.
Your options are really to
Save your figures as something else such as TIFF or PNG
You could try saving the figure as an EPS using export_fig from the file exchange but you will likely still have the same issue.
Turn off transparency, save to an EPS, and use Illustrator or an external program to change the transparency and try to save as an EPS file and see if you get better results.

How to improve the quality (smoothness) of lines in contourplots Matlab's

How the line smoothness in a contour plot can be improved for publications? For instance, the dotted lines look really bad, the continuous lines look as if their thickness varies. See below
Here's part of the code:
Vals = [0:5:200]; contourf(X,Y,W,Vals,'EdgeColor','k','LineWidth',1.2,'LineStyle',':');axis square;grid;hold on
Vals = [10:10:200]; contour(X,Y,W,Vals,'EdgeColor','k','LineWidth',1.2);
Vals = [20 : 20 : 200]; [C,h] = contour(X,Y,W,Vals,'Color','k','LineWidth',1.8);
clabel(C,h,'FontName','Palatino Linotype','FontAngle','italic','Fontsize',9,'Color','w')
print -djpeg -r300 filename
Thanks!
Saved as png doesn't help much... check the lines :/ See below:
Check the dotted lines now...
Here's saving as eps (-r1200)... it looks better
Exporting as vector graphics will definitely improve the image over what you see on your screen; I use LaTeX for publications and you can either export to eps for postscript output, and use epstopdf for PDF output, and embed these directly in your document; that would be the best solution.
Additionally, there are also a bunch of general utilities for making your plots look better for camera-ready publications, the most notable that comes to mind is exportfig, which has a load of features to help even with pixel graphics. These go above and beyond just generating smoother-looking images.
http://www.mathworks.us/matlabcentral/fileexchange/23629-exportfig
(copied from that page):
This function saves a figure or single axes to one or more vector and/or bitmap file formats, and/or outputs a rasterized version to the workspace, with the following properties:
Figure/axes reproduced as it appears on screen
Cropped borders (optional)
Embedded fonts (pdf only)
Improved line and grid line styles
Anti-aliased graphics (bitmap formats)
Render images at native resolution (optional for bitmap formats)
Transparent background supported (pdf, eps, png)
Semi-transparent patch objects supported (png only)
RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
Variable image compression, including lossless (pdf, eps, jpg)
Optionally append to file (pdf, tiff)
Vector formats: pdf, eps
Bitmap formats: png, tiff, jpg, bmp, export to workspace
This function is especially suited to exporting figures for use in publications and presentations, because of the high quality and portability of media produced.
Update: I see your example code now. Did you try changing -r300 to some really high value? More pixels per inch should make everything look smoother. For publication, crank it up really high, like -r1200.
Original:
One thing you can try is exporting the plot in some format that supports vector graphics. Matlab supports both PDF and EMF, so try one of those. Export using the saveas command or from the figure's "File -> Save as" menu item. After that, open or import the image file in some other application and hopefully it will look better.
Please add a new screenshot if you get a nicer image!

Getting rid of interpolation/aliasing in EPS export of matlab?

I have a 2D color-map plot created with imagesc and want to export it as a .eps file using
print -depsc.
The problem is that the "original" image data is from a rather small matrix (131 x 131). When I view the image in the matlab figure window, I can see all the individual pixels if I zoom a bit closer.
When I export to eps, however, there seems to be some interpolation or anti-aliasing going on, in that neighboring pixels get blurred/blended into each other. I don't get the problem if I export a high-resolution tiff, but that format is not an option (as demanded by a publisher).
How can I obtain an eps that preserves the pixely structure of my image without applying interpolation or anti-aliasing?
The blurring actually depends on the rendering software your viewer application or printer uses. To get good results all the time, make each pixel in your image an 8x8 block of pixels of the same color. The blurring then only affects the pixels at the edge of each block. 8x8 blocks are best as they compress without nasty artifacts using DCT compression (sometimes used in eps files).
Old question, but highly ranked in Google, so here is my answer:
Open the .eps-file with a text editor, search for "Interpolate" and change the following "true" to "false". Repeat that step for all Interpolate-statements.
It might also depend on the viewer you're using, but probably just because some viewers ignore the "Interpolate"s...
Had the same problem using plot2svg in Matlab and exporting from Inkscape to eps.