Insert vector graphics from Matlab to Word 2013 - matlab

i know its a often discussed topic. But i haven't found any solution for me. I have some plots, which i need to import in Word 2013 afterwards the docx is saved as pdf.
The problem is:
If the plot gets saved (with the function print() ) as .eps or emf and imported in Word, the downsampled placeholder of the graphics look pretty poor and the graphics in pdf are no vector grapic anymore. They do get turned into a bitmap and by zooming in a bit, one can see the pixels. If i use Edit -> Copy Figure in the Matlab figure and paste it in Word, i get a nice graphic in Word as well as a nice vector based graphic in the pdf file.
Is there a way, to save a figure to a file and import it to Word and have the same quality like i have by copy and paste? What is the difference between these methods?
Thank you for your help.
Rafael

Related

Differences between these two methods to include Matlab figures in a Powerpoint presentation

I want to include Matlab figures in a Powerpoint presentation. [Note that in other contexts, I usually use LaTeX beamer with .eps files, but here I am obliged to use Powerpoint.]
I know two ways:
Save the image as e.g. a .tiff file and include the image in Powerpoint. The result does not always look sharp and some lines are too thin.
Copy the image directly in Matlab and paste it in Powerpoint. The resulting figure is way nicer. But after exporting the Powerpoint to pdf, it may take some time to load the figure (especially when you included grid minor).
What is the difference between these two methods: is it the well-known bitmap vs. vector image difference or something else? Is there a way to have the quality of the second method but without the problem that is loads slowly?

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)

To imcrop non-textual PDF for Mathematica?

I want to prepare one selection of data from my high-quality PDF document which has no textual elements (just a plot), prepared originally by Matlab.
I do not want to give the whole picture for my collegues because it is too overwhelming.
#1 Tools in Matlab
I know this thread How can I read an image file that is stored in PDF format (much like reading a jpeg file with I = imread('image.jpg')? but I have got denying experiences from my colleagues and to my task PDF should be enough because my data is just a high-quality plot without textual elements.
Most relevant thread is this one How to extract data from pdf file in matlab?
Most attempts are based on extracting PDF to TXT, like How to Read PDF file in Matlab? about pdftotext.
I want now imcrop the PDF such that the output could be used in the time-series analysis of Mathematica here, but I did not find that the default imcrop tool of Matlab is supporting PDF, Crop an Image.
Some findings
Show and Save as PDF based on the answer. I do pdf = Import[filename.pdf]; Show[pdf[[1]], PlotRange -> {{50, 200}, {100, 300}}] and I see a good selected picture in Image viewer, but failure when exporting the picture back to Mathematica seeing the complete picture. Why? PlotRange does not crop but only put a white mask on the top of the picture which can separated etc in Mathematica.
Going from Show to ImageCrop based on this answer. Wrong approach, confusion with ImageTake.
Going from Show to ImageTake based on this answer.
The Show and ImageTake are not injective to each other because ImageTake has at least reversed order of parameters {ymin,ymax}, {xmin,xmax} according to the manual. However, I could not manage to select the correct selection by just reversing the parameters. Why?
Comments for Mathematica
It would be nice if the regions selected would correspond to each other.
Therefore, I would like to have some visual tool to select appropriate area from the figure.
I notice there occurs some aliasing when enlarging the original image.
It would be nice to know how Mathematica handles such cases with ImageTake.
How can you prepare imcrop of PDF image for the time-series toolbox of Mathematica?
I think this question is about image extraction.
However, I extended the question to the thread Better Colormap of Matlab and Image Extraction for Time-Series Toolbox of Mathematica? for Mathematica.
Mathematica will import your pdf as a graphic object which you can 'crop' using plotrange.
pdf = Import[filename.pdf];
Show[pdf[[1]], PlotRange -> {{50, 200}, {100, 300}}]
note the values are {{xmin,xmax},{ymin,ymax}} in "points"
You can also rasterize and then use ImageTake
ImageTake[Rasterize[pdf[[1]]], {10, 100}, {20, 100}]
here the values are {ymin,ymax} , {xmin,xmax} (note the reverse order )
Note the [[1]] here is effectively the page number. I'm pretty sure Import returns a list of pages even if the pdf is a single page.
If you want to actually extract plot data that's a whole other question. For that I'd suggest mathematica.stackexchange.com and provide an example file.

Add text to PDF

I've got an exported version of a MATLAB diagram, similar to the one below. The problem is, that there are no axis captions. It's not possible to export the file again from MATLAB. I need to edit the PDF programmatically and edit about 100 diagrams, all with the same axis positions.
Is there a clean and fast way to paste the Strings X and Y at the corresponding positions in the pdf based on a batch process?
Create a PDF file with the captions. Add that as a background with to the PDF files with pdftk.
if know how to use LaTeX, the pstool package can bring you far on this on, including replacing labels (or actually any text on an eps figure) with TeX symbolic expressions. Neat if you're already working in LaTeX.

MATLAB and high quality EPS figures

I am looking to export my MATLAB plot as a high quality figure. Specifically, I would like to save it as a vector based file format such as EPS or SVG.
I have tried print and saveas commands:
saveas(h,'myFileName','epsc2');
print('-r150','-depsc2', 'myFilename');
On all occasions this produces poor quality parts of the graph, although the axis-labels are indeed vector. Why does MatLab do some horrible rendering before putting it into an EPS?
Example of poor quality plot here:
http://users.ox.ac.uk/~pemb2372/myFileName.eps
Edit:
It is also worth noting that if you use a Mac viewing an EPS file from Matlab, 'Preview' app may render inner graph content rasterized and poor quality, while leaving the axis and labels vectorized and high quality. This is very misleading but when you open said EPS file in, for example, Inkscape, the quality is actually vector and quite high.
Edit 2:
My university hosting account has expired, so you can no longer view the figure. Suffice it to say that it showed a poor quality raster-style plot within high quality beautiful axis lines, ticks and labels.
I thought I would share the issue I had, and how I overcame it...
I was getting terrible results because I had the wrong renderer set to default. In my startup.m, I had the zbuffer renderer enabled. This is an example eps output.
I made that eps output with: print(gcf,'-depsc2','filename.eps'). This eps is so OBVIOUSLY rasterised. It makes me angry at matlab. Then, I had a brainwave - perhaps my default renderer zbuffer is interfering with the image save process. So, adding the line:
set(gcf,'renderer','painters')
and running the print command as before, here is the output:
Note that I just took screenshots of the eps output files at 100%. And I can confirm the second image is actually vector. Here is a good question/explanation on choosing Renderers in MATLAB.
Matlab can export to pdf with better quality than EPS, but with its own caveats of setting decent margins and font sizes.
edit:
Examples are similar to the EPS case as explained in the help of e.g. print:
saveas(gcf,'filename.pdf')
or
print('-dpdf','filename.pdf')
You might also want to take a look at the PaperSize, PaperPosition and PaperUnits properties of your figure (by means of the set and get functions).
edit: Another option is to use one of the functions available on FileExchange such as the ones mentioned by #user664303 below. My personal favorite for use with LaTeX is matlab2tikz for which the latest version can be gotten from GitHub. Together with the external library of TikZ, I think this delivers some of the most nicest graphs around.
Probably it's also best to mention that I have been actively involved in the matlab2tikz project since 2012.
The export_fig function on the MATLAB file exchange is a reasonably reliable way of accurately exporting figures to eps and pdf (as well as bitmap formats) in MATLAB.
The plot2svg function, also from the file exchange, allows you to export in svg format. It provides some additional benefits, such as being able to export translucent patch objects in vector format.
A comparison of exporting methods is given in this blog post.
I always acquire the final plots (those which are supposed to be inserted into papers and publications) by matplotlib library of python.
You can bet on the amazing quality of the generated plots, both .pdf and .eps formats.