03/06/2011

Simulink to eps

I've mentioned previously that I've developed a process to export plots from Matlab and include them as (eps format) figures in a Latex document. Well today I spent a load of time trying to get a similar method working for exporting Simulink model diagrams from Matlab. This hasn't proved quite as easy, but I think I'm there now.

My main issue was that Simulink doesn't allow you to print in the same way as a normal Matlab figure does. The "print" command can be used, but it works in a very specific way. It is fairly easy to use this command to print in eps format (see my previous posting for why this is a good thing); however it doesn't allow you the same freedom to setup the page. More specifically it will only allow you to use a pre-defined page size (A4, US legal, etc). It rescales and centres the figure to fill the page, but, unless the model is the exact same aspect ratio as a pre-set paper type, you end up with white borders at either the top/bottom or left/right.

EDIT: This seems to have been corrected in newer versions of Matlab now!

I've tried all sorts to get round this, including calling Ghostscript from within matlab to post-process the eps file. This should work as it ought to be able to find the right "bounding-box" for the figure and resize the page accordingly. However I had no luck with this - it would either not trim it down at all, or over-trim it removing some of the image.

I also tried exporting in a few other formats (namely normal postscript and pdf) and then converting with Ghostscript. This worked a little better and I was able to get the right bounding box through a process of:
  1. Simulink export to pdf, 
  2. Ghostscript convert pdf-to-eps, 
  3. Ghostscript convert eps-to-eps. 
I have no idea why that last step was necessary! Unfortunately although the output from this was pretty good, the text becomes part of the image (I think during the second step), meaning it doesn't render properly at high zoom levels. I don't know why that happened, I'm pretty sure it shouldn't have, but I thought I could do better.

So an alternative process I finally came up with is:
  1. Find out the ratio of the figure (I did this by exporting as a raster image and then reading back in - I'm not very happy with this technique as it's pretty dirty, but at least it works)
  2. Simulink export to eps, with the model at the very bottom of the page
  3. Automatically edit the resulting eps file to adjust its bounding box information
It's a bit hacky, and if I was being picky the margins aren't perfectly even in the resulting file, but it seems to work. I'm sure there must also be a better way of achieving step 1, but it's sufficient for now. (If anyone has any suggestions then I'd love to hear them!?)

There's nothing very clever in the code, but if anyone wants a copy of my function I'll happily forward it to them. Hope this helps somebody!

EDIT: Code for my function is available here.

5 comments:

  1. I have been hacking/screwing around lately in order to to get a lot of simulink schematics displayed in LaTex. It would be awesome if you could send me a copy of the function you use!

    Seppe

    ReplyDelete
  2. Seppe, I've put my function here. Like I say it's not a perfect solution, but I think it should work better than the alternatives. If you come up with any better approaches or improvements then please let me know!

    ReplyDelete
  3. Thanks, It seems to work rather well.. I changed the width/height multiplier constant to 0.6 instead of 0.99, this way the models can be fully displayed on A4 in portrait mode...

    Tomorrow I'll hopefully get some more info from a professor at my school who told me something about editing settings that define the default size of a model, I'll keep you updated if I find a more elegant solution.

    ReplyDelete
  4. Hello there welf,

    Just came across some solutions and found one to have nice results.
    The "print" command can actually export the model to a vector eps, as seen here: http://locksley90.blogspot.com.br/2008/11/exporting-simulink-models-to-eps-or.html

    Hope it helps you and your readers... Helped me for sure! =]

    PS.: The command on the URL will convert only the main model. If you, like me, pefers to organize the model in subsystems, just add "/subsystemname" to the model name on the print command and it will convert just the desired one. Borrowing the example:
    print -ssimmodel/yoursubsystemname -deps -r300 simmodel.eps

    Greetings from Brazil.

    ReplyDelete
  5. Hi there,

    This is the exact method that I used, however I also included some extra steps to reduce the size of the final images (otherwise it comes out with very large margins).

    I've just retested in a newer version of Simulink (r2012a) and it seems that the extra steps are no longer necessary. I guess they must have improved/corrected the way the bounding box was calculated.

    Thanks for prompting me to check this!

    ReplyDelete