Copy figure in Matlab - matlab

I am asking for a solution to the blurred image I get when I use copy figure option in Matlab. For example, when I plot 3d figure in matlab I get the following image (It is the result of printscreen!)..
However, when I use the option copy figure I get a blurred image as the following image
.
How can I avoid this as I need to get high resolution image using copy figure option. Thank you in advance for any help you may lend.

By default, the figure is copied as an Enhanced Metafile (a color graphics format) which works for basic plots such as bar plots, line plots, and other 2D plots but may yield undesirable results for more complex plots as in your case. This is because the rendering of the Metafile content is controlled my Microsoft Word and it may render things differently than MATLAB.
If you need a high resolution image, I would recommend using the print command where you can specify the desired resolution (using the -r option). Then you can import the resulting image into Word
print(gcf, 'myfile.png', '-dpng', '-r300')
Alternately, you can use export_fig from the File Exchange which better preserves the appearance
Another option would be to change the format used by Copy Figure to tell it to use a bitmap instead
Or you can call Copy Figure programmatically and specify a different format (either a bitmap or PDF)
print(gcf, '-clipboard', '-dpdf')

Related

Matlab plot saved as (vector) EMF but rendered as raster

I have Matlab code that produces an array of subplots that looks like this:
When I save this as an EMF file (normally a vector format) and insert in Powerpoint or view with Inkscape, then zoom in closely on the image, it looks pixelated, and clearly made up of a single encapsulated object as opposed to many small graphical objects:
This is very surprising to me, as I have for a long time been exporting Matlab plots as EMF and integrating those into Powerpoint slides, in order to build there more complex graphs that, as vector graphics, would print well at any size. These imported EMFs would also scale well and look smooth regardless of how much I zoomed into them in Powerpoint.
Can anyone guess why it is that on this occasion, this plot is not saved as a vector graphics but is instead (as it seems) rendered as raster? The code that produces this figure (based on the Matlab commands subplot, line, and scatter) is rather long/inelegant, but I can give details or simplify, if required to find a solution. Thanks!
Other similar threads on this site have not helped fix this.
I believe I have encountered this issue before. If it is the same as the issue I was facing, it is to do with the renderer being used to save the plot. Although it should automatically select the painters renderer when exporting to vector files, I have had instances where it used the openGL renderer instead, which results in bitmaps being used. I'm not entirely sure why this happens -- it might be the case that for particularly complicated figures, it reverts to openGL to avoid obnoxiously large or complicated vector files.
If you are using the print command, you can force it to use the painters algorithm as follows:
print('-painters',...)
if you're saving using File>Save As..., I believe setting the renderer for the figure should work:
set(figure_handle,'renderer','painters');
For explanation, per the MATLAB documentation:
-opengl' — OpenGL renderer. Use this renderer when saving bitmap images. OpenGL produces a bitmap image even with vector formats, which might limit the extent to which you can edit the image in other applications.
'-painters' — Painters renderer. Use this renderer when saving vector graphics files. If you save to a vector graphics file and if the figure RendererMode property is set to 'auto', then print automatically attempts to use the Painters renderer. If you want to ensure that your output format is a true vector graphics file, then specify the Painters renderer. For example:
print('-painters','-deps','myVectorFile')
If you do not specify the renderer, then print automatically uses the appropriate renderer to produce the output format requested. However, if you set the Renderer property for the figure, then print uses that renderer when generating output.
EDIT: Another option is to use the Copy Figure command (Edit > Copy Figure) -- this should copy the figure as an EMF file, and should obey the figure's renderer settings.

Matlab: Export a figure made with myaa

I am trying to get better quality phase plots of complex functions made with the Complex Function Explorer of E. Wegert CFE. For this purpose I apply the Matlab anti-aliasing function myaa.m to the phase plots that are made with the CFEGUI.m. For the example screenshot of the result window below I used the setting myaa([8 8]) in the Matlab command window which means that the supersampling enlarge the figure 8x and then downscale it to 1/8 to get the original h x w.
As one can see the window of the figure has no operation icons or menu options for save or print. My question is what to do with such a figure (beside making screenshots)? Can I somehow use export-fig to save such a figure or load it in an image array and if yes, how?
It is also possible to use the setting myaa([8 1]) which results in a very large figure window that is larger than my screen (and has the unpleasant attribute that it can not be moved). It would be even better if such a whole figure could be saved (not only the visible part).
You can use getframe to grab current figure information in pixels.
However, while I do not know how this affects you, notice that MATLAB versions above 2014b (included) have already anti-aliasing. The code you linked seem to be from 2008, I am not 100% sure if it has become obsolete now.

Matlab figure saved as .eps appears in 'horizontal sections' when opened in Adobe Illustrator

I normally save my matlab figures as .eps and then make them better looking using Adobe Illustrator. This works for most figures but not all.
For example I plotted my data using the violin.m function from file exchange. When I save it as .eps and open it in Illustrator, I don't get an editable figure as I usually do. Instead, My figure appears chopped in horizontal sections, and all I can do is delete them (like in the figure below).
Is there anything I can do in either matlab or Illustrator to be able to edit the figure?
I don't need an .eps file, I just need to be able to edit it.
EDITS:
I tried #MattSchmatt's suggestion of using the print2eps function but I had the same problem.
Saving as .pdf doesn't solve the problem, because the image I get is not editable in Illustrator (plus, I also get the horizontal 'chunks').
A minimal, complete and verifiable example requires matlab, the violin function linked above and illustrator. But if it helps, here's the matlab code to produce a similar figure. I save by clicking on Figure -> save as. (But as I said above I tried the print2eps function and that was the same).
X = rand(100,6);
figure; violin(X)
I tried the following, didn't work either.
set(gcf, 'Renderer', 'painters')
As the author of the question suggests, I also wasn't able to export an editable (vectorized) .eps file into Illustrator. However, exporting it as .pdf does the trick. The 'fill' for the violin plots is weird, though, and has horizontal sections (perhaps something to do with the way the density estimate is being plotted?) all over. I was able to fix this and make the plots normal (with solid filled shades): select all the horizontal sections/chunks using the magic wand tool, and then increase opacity to 100% (it is set to 50 based on the exported file). Once all the required edits are made, export the file as .tiff and it seems to look fine. Hope this replicates, and thus helps! (my MATLAB version is 2016, and Illustrator version is the CS5)

Matlab imwrite() quality

I'm very new to Matlab, though I know a few other programming languages, so please forgive me if this is something simple. I have not been able to find any answers to this, either on StackOverflow or elsewhere.
I produce a figure using the following code:
figure(6),imageplot(P); drawnow;
Which looks like this:
I then save this image to my computer using the following commands:
imwrite(P, 'images/plot.png');
And the resulting image is tiny, and missing some of the color information:
If, however, I utilize the save function in the open figure (image #1) and save it manually, I get exactly what I want, which is that exact image stored on my computer.
How would I program that? I assumed that imwrite() would just write the image directly, but apparently I'm doing something wrong. Any advice? Perhaps it has something to do with the imageplot command? I cannot seem to get that to work in imwrite.
Update: Based on the comments below, I have begun using "imresize" with the "nearest" option. This scales the image properly, but the resulting image is still curiously darker (and therefore has less information) than if I hit the "save" button in the figure.
Image saved from figure:
Image using "imresize" with "nearest" option:
The MATLAB imwrite command saves exactly the number of pixels as specified in your image matrix. This is the actual result of your computation; the reason the output is "tiny" is because it is supposed to be. To make it larger, would be to simply scale/zoom it as required.
The save figure option however does something quite different: it rasterizes the the output you obtain in the figure window and gives you the option for saving it as an image. This is evident in the fact that when you do so, you obtain a white background in addition to your result which is really just the grey background you see before you save it; this can be adjusted by resizing the figure window before utilizing the save option.
If you're looking to simply make the output figure larger, I would recommend using something along the lines of the imresize command.
Say, if you want the default size to be twice the size of the real result, simply use:
imresize(P, 2.0);
For more options, try help imresize.
The command you need for the "Save As..." functionality of figures is called "print". I often use
print(gcf, '-dpng', 'some_filename.png')
or
print(gcf, '-depsc', 'some_filename.eps','-r0')
to save a figure as it is shown on screen. The format png offers a small filesize and excellent quality, and it is understood by most image viewers and browsers. The eps format is a vector format, which I use for printig. The '-r0' option specifies "use the same size as given by the screen resolution" for the vector format properties.

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.