4

I am writing my Thesis report using LaTeX. I have to explain about a few models that I created in Simulink in my report. I do not know how to import these Simulink models into LaTeX. Can someone help me out with this?

1
  • I think you have two options: (1) make a snapshot and insert the picture in your LaTeX file or (2) export the Simulink model to HTML, then convert to LaTeX: see here for this last step.
    – Count Zero
    Commented Jun 7, 2013 at 12:24

5 Answers 5

7

You can do the following; I use Matlab 2013, but I suspect that can also be done in older versions.

  1. Go to File >> Print, then click on Print.
  2. Under Device option, write -dpdf.
  3. Under Send to, you have a choice of two radio buttons; select File.
  4. Press Print; a dialog box will prompt you to choose a path for the file.

Then, in LaTeX, simply use \includegraphics from the graphicx package to insert the pdf you have just saved.

Note: In my experience, most people tend to insert Simulink diagrams as raster-graphics images of dimensions so small that the model is illegible, even when you zoom in on it. That problem is easily obviated by using a vector-graphics format (e.g. pdf), which I urge you to do.

4
  • Take note that converting the block to pdf gives you some white spaces around the figure. Commented Jun 7, 2013 at 13:06
  • @HasanZakeri Yes. That's what I was trying to fix. Matlab/Simulink doesn't seem to offer much flexibility in this regard. The only workaround is to modify the paper size option in the Print window.
    – jub0bs
    Commented Jun 7, 2013 at 13:08
  • 1
    Or remove the surrounding white space with external tools, eg. ghostscript :) Commented Jun 7, 2013 at 16:39
  • I sometimes use Adobe illustrator to crop pdf files.
    – Ali
    Commented Nov 9, 2013 at 12:16
2

First, save your Simulink model as a figure using Matlab command:

saveas(h,'filename.ext')

or

saveas(h,'filename','format')

following the directions in this MathWoks link.

In you LaTeX file use (if you saved your Simulink model as an eps file):

\begin{figure}
  \centering       
    \includegraphics{myfile.eps}
\end{figure}

Also, this site can be of interest.

3
  • This works only for figures. The actual model cannot be saved this way.
    – Count Zero
    Commented Jun 7, 2013 at 12:58
  • @CountZero: Yes. I write in the answer (if your saved your Simulink model as an eps file). Thanks.
    – user13907
    Commented Jun 7, 2013 at 13:02
  • 1
    @Papiro: Thanks, especially for the quick response. Commented Jun 7, 2013 at 13:35
2

Quick and easy solution to save Simulink figures as eps:

The above mentioned solutions didnt work or were not exactly what I wanted so here is my solution.

Step 1: Have your Simulink project open and type in the MATLAB Workspace: print -dmeta -s dummy.emf and change dummy to the name of your Simulink file.

What this does is to save your Simulink figure in the emf file format.

Step 2: Convert the emf file to eps:

Use an online converter like this

(Step 3:) Use \includegraphicsas always to place your figure in your LaTeX document.

Advantages over the other methods:

  • Automatically removes unnecessary white spaces
  • No need for additional Mathworks code piece
  • Easy to follow for beginners like me
0

I always use this Mathworks code piece. It removes additional space and works with all figures, I encountered.

1
  • 2
    Unfortunately, it works with figures not simulink models
    – percusse
    Commented Jun 7, 2013 at 13:51
0

Snapshot of a Simulink model:

  • Open the Simulink system which you would like to print to file.
  • Switch to the Matlab application and run the following code:
    • print -dpdf -s <outputFileName>.pdf %vector format
    • print -dpng -r225 -s <outputFileName>.png %raster format

For vector format: I'm using r2013b, which has pdf capability despite the documentation completely omitting it. Your usage may vary.

For raster format: The default resolution, -r90 can be terrible. Look for washed out connection lines. I found this to occur straight through -r200 when creating .png files.

Snapshot of a Stateflow model:

  • Open the Stateflow system which you would like to print to file.
  • Switch to the Matlab application and run the following code:
    • sfprint(gcs, 'png', '<outputFileName>.png')

For vector format: sfprint does not yet have .pdf output capability. You'll need to use .eps or .svg and a converter. Packages exist within the latex environment to make the conversion. Even so, you may not want latex to convert each time you compile. [See Feature Requests, below.]

For raster format: sfprint does not yet have an option to specify resolution. Even so, I believe this command still provides improved resolution over the File --> Print method.

.PDF Autocrop

To crop your .pdf down to its minimum margins, use the following code on the output .pdf from print:

  • !pdfcrop <outputFileName>.pdf <outputFileName>.pdf

Feature requests

In order to improve the process of exporting images from Mathworks products to a latex environment, I have recently requested that the Simulink and Stateflow teams consider the incorporation of additional functionality to the print and sfprint functions in future releases.

From what I understand, these functions have seen improvements over the past few years already, and I would thus consider them still open to additional development.

I requested the following features:

print:

  • option to automatically crop margins/whitespace.
  • option to specify margins/whitespace of crop option.

sfprint:

  • option to print to .pdf. [option already exists in print].
  • option to specify raster image resolution. [option already exists in print].
  • option to automatically crop margins/whitespace.
  • option to specify margins/whitespace of crop option.

Contact Mathworks with additional insight.

My scope only considered pdflatex. Additional input from the community would likely be useful. Many of you may have needs which are not clear to me which would be better represented yourself. Your submissions would also demonstrate that support for the development of these features would be worthwhile.

To: US MathWorks Support <[email protected]>
Re: Specifying image resolution with sfprint [ ref:_00Di0Ha1u._500i0THMmD:ref ]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .