Graphics, Figures & TablesHow to import Matlab figures into Latex?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sercandemir
Posts: 3
Joined: Mon Jan 19, 2015 6:52 pm

How to import Matlab figures into Latex?

Post by sercandemir »

Hello, I have a bunch a graph in Matlab and I am trying to import them to Latex.
I tried to save the graph as png and pdf files.
I use the code below:

Code: Select all

 \begin{figure}
  \centering
  \includegraphics{f1.1}
  \caption{graph1}
\end{figure}
Latex gives me this error:

Code: Select all

LaTeX Error: Unknown graphics extension: .1.
See the LaTeX manual or LaTeX Companion for explanation.
Type H return for immediate help.... 
\includegraphics{f1.1}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to import Matlab figures into Latex?

Post by Johannes_B »

Hi and welcome,

You tried to make your filenames more clear using periods, bad idea. LaTeX recognizes your 1 (one) as the file ending, which it doesn't recognize.

Try loading package grffile.


I don't know how matlab works, but exporting to pdf (a vector format) is always better. You could export you data as well, and plot it using LaTeX package pgfplots. Pleas have a look at the examples at pgfplots.net.

Edit: Maybe you need to try what David Carlisle suggested:

Code: Select all

\includegraphics[type=png,ext=.png]{f1.1}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
sercandemir
Posts: 3
Joined: Mon Jan 19, 2015 6:52 pm

Re: How to import Matlab figures into Latex?

Post by sercandemir »

Hello. Thank you for the reply. Now, I import my graph to latex. However, I have another problem. The figure comes separate the paragraph and located between sentences. It is weird because the code for the figure is located under that paragraph.
I tried to resize the figure but it doesn't work.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to import Matlab figures into Latex?

Post by Johannes_B »

You are using floating environments, they do that, it is their nature.

Please read in introduction about floating. You are then prepared for How to influence the position of float environments?.

You can prevent the behaviour, but this will result in quite some uglyness.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How to import Matlab figures into Latex?

Post by Stefan Kottwitz »

Captions and also references are there because it's very common that figures are placed where it's optimal for page breaking and layout proportions.

A bit stretched: the text may say "See figure 1, which describes the process X." Then the figure follows, with a caption Figure1: The process X". And possibly much whitespace after the text before the figure with a page break, because it turned out that the figure doesn't fit well any more to the page after inserting other text... That's why figures are often at the top of a new page, but text is floating before. Anything can move a bit. We got references for pointing to the figures, and captions there to explain them. No need to stick figures at fixed points within the body text.

Often it's sufficient to relax the positioning a bit via options, up to:

Code: Select all

\begin{figure}[!htbp]
...
\end{figure}
This allows positioning anywhere (here, top, bottom, dedicated page) and relaxed typographic recommendations. Default it's just ht for here or top.

Stefan
LaTeX.org admin
Post Reply