3
votes

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:

  1. For figures, there are solutions to this question. I have a Simulink block diagram, it's different (see below).
  2. I am aware of solutions using additional software like pdfcrop.
  3. 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.
3
What's wrong with cropping the pdf afterwards? There are some tools - pdfcrop or Briss.remus
There are two problems. print scales the model as it thinks and I could not find a way to get the scale factor out of it. So by cropping it's not done. Say I want 10pt font in the Simulink model to look the same for a number of models later in my document. How do I do this? I'm writing a tool for other people to use. They don't want to do stuff by hand and they don't like installing additional software, which is problem number two.user3215534
This should effectively give one the same result: tex.stackexchange.com/questions/57418/crop-an-inserted-imagemue
This should give one effectively the same results: tex.stackexchange.com/questions/57418/crop-an-inserted-imagemue

3 Answers

1
votes

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.

1
votes

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!

0
votes

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');