Fix extra space in MATLAB's title plot - matlab

I'm writting the results for my thesis, this includes the generation of figures for my LaTeX document by using MATLAB code. I do this by making a figure of the data, and then I use the print command to save in an EPS file.
The problem is that the plot in the MATLAB window is correct as you can see here:
But when I compile my document in LaTeX (Lyx), the result is this:
.
As you can see, I have an unexpected big extra space in "iLm" title. The same occurs when I use LaTeX code in the label of different signals.
Searching in the web I tried the following command:
set(groot,'DefaultTextInterpreter','latex');
But just prints "iL_m" like I wrote in the code. How can I make the spacing consistent in the EPS file?
Here's the code I'm using:
clear h n
figure(1)
h(1) = plot(iLmVal.time,iLmVal.data(:,2),'LineWidth',1,'color','k','DisplayName','Modelo');
hold on
h(2) = plot(iLmVal.time,iLmVal.data(:,4),'LineWidth',1,'color','r','DisplayName','Circuito');
legend(h,'Location','southeast'),...
axis([0 0.06 -18 27]),title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times-Roman'),...
set(gca,'Color','white');
set(gca,'XTick',0:0.005:0.06),...
set(gca,'XTickLabel',0:5:60,'FontSize',20,'FontName', 'Times-Roman','XMinorGrid','on'),...
xlabel('Tiempo [ms]','FontSize',20,'FontName', 'Times-Roman'),...
set(gca,'YTick',-18:4:28),...
set(gca,'YTickLabel',-18:4:28,'FontSize',20,'FontName', 'Times-Roman','YMinorGrid','on'),...
ylabel('Corriente [A]','FontSize',20,'FontName', 'Times-Roman'),...
n = gca;
n.YAxis.MinorTick = 'on'; n.YAxis.MinorTickValues = -18:1:28;
n.XAxis.MinorTick = 'on'; n.XAxis.MinorTickValues = 0:0.0025:0.07;
grid on; hold off
I'm using MATLAB R2018a and Lyx 2.3.2-2. Also, by printing in PNG this problem doesn't occur, but the quality and resolution is very poor.

I don't think this is related to LyX, you should see this issue in the exported EPS file. You can fix this using a different font.
As you can see in the appearance of the figure in MATLAB, where the title is shown using a sans-serif font (definitely not 'Times-Roman'), MATLAB does not recognize the 'Times-Roman' font, and uses an alternative for rendering. This alternative font is used to determine the location of the subscript, which is positioned independently of the main text by MATLAB. However, this font name is written to the EPS file. When rendering the EPS file in a different program, the 'Times-Roman' font is recognized and used to render the text. Because this font has different metrics to the one used by MATLAB, the location of subscripts is not correct.
When printing to PNG, MATLAB creates a bitmap, therefore this problem does not occur.
On my computer (macOS), the fooling produce a correct representation on screen:
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times')
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times-Roman')
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','TimesRoman')
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times New Roman')
The following doesn't:
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times Roman')
On different computers, different font names will be available. Use a name that is recognized on your computer. Your best bet is 'Times', which is the PostScript name for this font and should be recognized everywhere.
Alternatively, use the export_fig utility on the File Exchange. This is a great tool for exporting MATLAB figures to EPS. It will not only fix your fonts, but make many other little tweaks that will improve the look of your figures.

Related

Saving figure to pdf prints # instead of tabs in titles

if I create a plot with some tabs in the titles and try saving that to a local pdf file (via print function), I get some hashtags instead of tabs in the pdf. This does not occur in the visible figure.
For example, I plotted the residuals of an approximant to the runge function on a grid:
plot(g, f(g) - runge(g));
title(sprintf('residuals,\t max_x(s-f) = %.3f', max(f(g)-runge(g))));
Then after some axes manipulation (grid, boxes, etc..) I execute
h = gcf;
set(h,'Units','Inches');
pos = get(h,'Position');
set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(h,'data/runge_example.pdf','-dpdf','-r200')
close(h)
Is somebody aware of that behaviour or better able to found that as already solved than I am?
EDIT: Same behaviour with saveas and saving to eps. This does not happen with \n
EDIT2: I'm using Matlab Version R2017b (9.3.0.7...)
So, after a long discussion we figured out that we have no idea of how to use tabs in matlab titles in combination with latex mode.
To clarify: use math environments and escape tex directives, otherwise it will throw an error.
so
sprintf('residuals,\t $max_x(\\vert s-f\\vert) = %.3f$', max(f(g)-runge(g))));
will give you a nice string:
residuals, $max_x(\vert s-f\vert) = 0.264$
The problem is, that it really is a tab. And the matlab latex interpreter (don't know which one that uses, the system or an own) crashes on that. I copy-pasted that to a tex document and pdflatex ran fine on it (but not showing that much space unfortunately).
So, I came up with the following fix:
use the latex directive \quad or \qquad:
title(sprintf('residuals,\\quad $max_x(\\vert s-f\\vert) = %.3f$', max(abs(f(g)-runge(g)))));
This will give you more space than a normal space.
EDIT: For this to work you need the interpreter of matlab to be set to "latex" instead of the default "tex". Do this by changing the title to
title(title_string, 'Interpreter', 'latex')
or by setting (globally for that script)
set(groot, 'defaultTextInterpreter', 'latex')

Matlab: loss of Latex font on saving eps file

In Matlab, I am using LaTex interpreter as default and CMU Serif as in Latex as my default font through the following commands inside startup.m.
set(0,'DefaultTextFontname', 'CMU Serif');
set(0,'DefaultAxesFontName', 'CMU Serif');
set(0,'defaulttextinterpreter','latex');
The plot produced from Matlab appears like this, with the fonts in place:
However, upon saving the above image as eps using either print or saveas command produces figure with all the ticks and label in a different font:
Can someone suggest, what is the issue resulting in different fonts?

Exporting figures as vector graphics in .pdf-format using HG2-Update and 'painters' renderer is not working properly

I'm using the still undocumented HG2-Update to create my MATLAB plots, because they just look that much nicer.
(Source: Yair Altman)
Actually, using the current version Release 2013b it works quite nicely and there are not much issues. Except one wants to export the figures as vector graphics (renderer: '-painters'), especially as pdf.
I use the commands:
saveas(gcf,'test.pdf','pdf')
or
print(gcf,'test.pdf','-dpdf')
There are rendering issues, the print does not contain the whole figure and some parts are cropped or non-default fonts are not recognized.
But I'd really like to stay with HG2 and I'd still like to use vector graphics. Is there any solution or workaround?
Exporting vector graphics using the yet not official HG2-Update is quite an issue. The .pdf-export is still totally screwed up.
What is working fine is the .svg-export, apart from that the boundary box is not set properly.
The long workaround would be:
Save the plot with '-dsvg' (print-command) or 'svg' (saveas-command) as vector graphic, open the file in the open source application Inkscape and save again as .pdf with the Export area is drawing checkmark set.
Quite complicated, so I found a way to do it via command-line directly from Matlab (Inkscape still required!):
filename = 'test';
inkscapepath = '"C:\Program Files (x86)\Inkscape\inkscape.exe"';
%// save as .svg
saveas(gcf,filename,'svg')
%// open and save with "export-area-drawing" set via command line
system( [inkscapepath ' ' filename ...
'.svg --export-area-drawing --export-pdf=' filename '.pdf'])
It takes some time, but works without any known issues for now.
Additionally delete the svg-File afterwards:
delete([filename '.svg'])
I had the same problem and used the workaround from thewaywewalk. Now I discovered the MATLAB function "hgexport" works under HG2 (in R2014a).
An issue still was the paper size. I want to use the same size for all graphs with as little white frame as possible. Here you have to set two sizes:
The papersize is set with set(gcf,'PaperSize',[width height]) and the size of your chart is set through export styles. These are set in "Export Setup" or command line:
exp_style=hgexport('readstyle','default');
exp_style.Width = 'width';
exp_style.Height = 'height';
exp_style.Renderer = 'painters';
Now you can export your pdf:
hgexport(gcf,'pdfname',exp_style,'Format','pdf');
PS: In HG2 you may also use Latex for tick labels:
set(gca,'TickLabelInterpreter','latex');

Save Matlab Simulink Model as PDF with tight bounding box

Given a Simulink block diagram (model), I would like to produce a 'Screenshot' to be used later in a LaTeX document. I want this screenshot to be PDF (vector graphic, -> pdflatex) with a tight bounding box, by that I mean no unneccessary white space around the diagram.
I have searched the net, searched stackexchange, searched the matlab doc. But no success so far. Some notes:
For figures, there are solutions to this question. I have a Simulink block diagram, it's different (see below).
I am aware of solutions using additional software like pdfcrop.
PDF seems to be the only driver that really produces vector graphics (R2013b on Win7 here). The EPS and PS output seems to have bitmaps inside. You zoom, you see it.
What I have tried:
1.
The default behaviour of print
modelName = 'vdp'; % example system
load_system(modelName); % load in background
% print to file as pdf and as jpeg
print(['-s',modelName],'-dpdf','pdfOutput1')
print(['-s',modelName],'-djpeg','jpegOutput1')
The JPEG looks good, tight bounding box. The PDF is centered on a page that looks like A4 or usletter. Not what I want.
2.
There are several parameters for printing block diagrams. See the Simulink reference page http://www.mathworks.com/help/simulink/slref/model-parameters.html. Let's extract some:
modelName = 'vdp'; % example system
load_system(modelName); % load in background
PaperPositionMode = get_param(modelName,'PaperPositionMode');
PaperUnits = get_param(modelName,'PaperUnits');
PaperPosition = get_param(modelName,'PaperPosition');
PaperSize = get_param(modelName,'PaperSize');
According to the documentation, PaperPosition contains a four element vector [left, bottom, width, height]. The last two elements specify the bounding box, the first two specify the distance of the lower left corner of the bounding box from the lower left corner of the paper.
Now when I print the PDF output and measure using a ruler, I find the values of both the bounding box and the position of its lower left corner are totally wrong (Yes, I have measured in PaperUnits). That's a real bummer. I could have calculated the margins to trim off the paper to be used later in \includegraphics[clip=true,trim=...]{pdfpage}.
3.
Of course what I initially wanted is a PDF that is already cropped. There is a solution for figures, it goes like this: You move the bounding box to the lower left corner of the paper and than change the paper size to the size of the bounding box.
oldPaperPosition = get_param(modelName,'PaperPosition');
set_param(modelName,'PaperPositionMode','manual');
set_param(modelName,'PaperPosition',[0 0 oldPaperPosition(3:4)]);
set_param(modelName,'PaperSize',oldPaperPosition(3:4));
For simulink models, there are two problems with this. PaperSize is a read-only parameter for models. And changing the PaperPosition has no effect at all on the output.
I'm running out of ideas, really.
EDIT ----------------------------------
Allright, to keep you updated: I talked to the Matlab support about this.
In R2013b, there are bugs causing wrong behaviour of PaperPositionMode and the bounding box from PaperPostion to be wrong.
There is no known way to extract the scale factor from print.
They suggested to go this way: Simulink --(print)--> SVG --(Inkscape)--> PDF. It works really good this way. The (correct) bounding box is an attribute of the svg node and the scale factor when exporting to SVG is always the same. Furthermore, Inkscape produces an already cropped PDF. So this approach solves all my problems, just you need Inkscape.
You can try export_fig to export your figures. WYSIWYG! This function is especially suited to exporting figures for use in publications and presentations, because of the high quality and portability of media produced.
Why you don't like to use pdfcrop?
My code works perfectly, and everything is inside Matlab:
function prints(name)
%%Prints Print current simulink model screen and save as eps and pdf
print('-s', '-depsc','-tiff', name)
print('-s', '-dpdf','-tiff', name)
dos(['pdfcrop ' name '.pdf ' name '.pdf &']);
end
You just have to invoke pdfcrop using "dos" command, and it's works fine!
on 2021a you have exportgraphics.
beatiful pdf images.
figure(3);
plot(Time.Data,wSOHO_KpKi.Data,'-',Time.Data,Demanded_Speed.Data,'--');
grid;
xlh = xlabel('$\mathrm{t\left [ s \right ]}$','interpreter','latex',"FontSize",15);
ylh = ylabel('$\mathrm{\omega _{m}\left [ rads/s \right ]}$','interpreter','latex',"FontSize",15);
xlh.Position(2) = xlh.Position(2) - abs(xlh.Position(2) * 0.05);
ylh.Position(1) = ylh.Position(1) - abs(ylh.Position(1) * 0.01);
exportgraphics(figure(3),'Grafico de Escalon Inicial velocidad estimada por algoritmo SOHO-KpKi.pdf');

How to export non-blurry eps images?

I'm exporting an image in Matlab using the eps format, but it smooths the image. Matlab does not blur the image using other formats such as png. I would like to know how to export a non-blurry image with eps format. Here is the resulting image using png:
And here is the resulting image using eps:
UPDATE:
The problem is reproducible on a Mac, and the issue is with the eps renderer rather than MATLAB. For e.g., saving imagesc(rand(20)) and viewing with Preview and GSview results in the following:
Preview screenshot
GSview screenshot
Clearly, the information is not lost. It is just not interpreted/read correctly by some EPS viewers. The solution is simple: use GSview to view your eps images. You can download it from here
On Macs especially, if your end application is latex/pdflatex, you will have to explicitly set it to use GS/GSview, because otherwise, it will default to the Quartz engine, which is baked into the OS.
PREVIOUS ANSWER:
I am unable to reproduce the behavior your described. Here is the code I used, tested using R2010b on WinXP 32-bit:
M = fspecial('gaussian',[20 20],5);
imagesc(M)
print('-dpng','a.png')
print('-depsc2','b.eps')
a.png
b.eps
Perhaps this is an issue with your EPS viewer...
not sure why it works but you can try doing the following:
eps2eps oldfile newfile
does the trick for me (on a mac os)
At first I thought you were doing something incorrectly, but then I remembered that this was an issue that was bothering the hell out of me a year or so ago. I couldn't come up with a way to "fix" this behaviour and from what I've researched, this is most likely a bug and several others have had this problem too and there is no known solution. Of course, I could be wrong about the last part and there might be solutions out there that have come out since I looked for them.
Any way, my workaround this problem was to use pcolor with shading flat instead of imagesc. When you export this to an eps format it preserves the image correctly. Example:
pcolor(rand(20));
shading flat
print('-depsc','figure.eps')
NOTE: You might see the appearance of thin, faint white lines along the anti-diagonals of each little square (depends on the OS & viewer). These are the edges of the graphics primitives that are used to render the image. However, this is not a flaw in MATLAB's export, but rather a fault in rendering in your EPS/PDF viewer. For e.g., with the default settings in Preview on my mac, these lines show up, whereas with the default in Adobe Reader 9.4, they don't appear.
If anyone is still interested in a workaround: Open the .eps-file with text editor and search for "interpolate". You'll probably find "/Interpolate true def" two or three times. Replace "true" with "false" and be happy :)
A note regarding Yoda's answer: in Preview in Mac OS X, you can make the thin white diagonal lines across each of the squares disappear by unchecking "Anti-alias text and screen art". Of course, the downside is that then any text (e.g. figure axes, etc) is not anti-aliased. Unfortunately, unchecking that has no effect on blurriness if you're using imagesc.
Another note is that if you use preview to make a pdf from your eps, the resulting pdf still displays correctly (non-blurry) when you open it in Acrobat.
I've been long struggling with this problem as well. So far, GSView is the only viewer I've found that displays the eps figures produced by Matlab (R2015b) correctly. eps2eps did not work for me (psutils 1.23).
The following eventually worked for me:
Export the figure to pdf, following the instructions here
pdf2ps file.pdf file.eps
I just wrote this simple drop-in replacement for imagesc. It doesn't support all but the most basic features, but I still hope it helps.
function h = imagesc4pdf(C)
[ny nx] = size(C);
px = bsxfun(#plus, [-0.5; 0.5; 0.5; -0.5], reshape(1:nx, [1 1 nx]));
py = bsxfun(#plus, [-0.5; -0.5; 0.5; 0.5], 1:ny);
n = numel(C);
px = reshape(repmat(px, [1 ny 1]), 4, n);
py = reshape(repmat(py, [1 1 nx]), 4, n);
h = patch(px, py, reshape(C,1,n), 'linestyle', 'none');
xlim([.5 nx+.5]);
ylim([.5 ny+.5]);
set(gca, 'ydir', 'reverse');
Apply opengl renderer to the figure
figure(gcf);
set(gcf,'renderer','opengl');
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, i.e. resize the image like this:
im2 = imresize(im1, 8, 'nearest');
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).
This page helped me a lot: http://tech.mof-mof.co.jp/blog/machine-learning-octave.html (written in Japanese, please use google translate for it)
And this is also helpful: Octave-Gnuplot-AquaTerm error: set terminal aqua enhanced title "Figure 1"...unknown terminal type"
I also answered at https://www.coursera.org/learn/machine-learning/discussions/weeks/2/threads/Dh-aRfqSEeaHSQ6l4xnh6g.
I reinstalled gnuplot like this:
$ brew cask install xquartz
$ brew cask install aquaterm
$ brew uninstall gnuplot
$ brew install gnuplot --with-aquaterm --with-x11 --with-qt # you can show other options by `$ brew options gnuplot`
You may edit ~/.octaverc like this:
setenv("GNUTERM", "qt")
and in octave window, after typing "system gnuplot", then
set pm3d interpolate 2, 2
After saving the file, open octave-cli.app, and type
imagesc(magic(3)), colorbar
I got this.