Trinh @ Bath

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
it_matlab_figures [2024/03/23 23:28]
trinh
it_matlab_figures [2025/02/07 16:06] (current)
trinh
Line 1: Line 1:
 ====== Generating nice images in Matlab ====== ====== Generating nice images in Matlab ======
  
-You may be using Matlab to create graphicsSome considerations:+Here are some brief details and tips for developing a good workflow for generating publication-quality images
  
-  - Save all data or all scripts. Get in the habit of save data as `.mat` files and scripts that will plot the data. This means if you ever need to regenerate pictures, you can do it very easily.+===== A workflow for saving images =====
  
-  - Similar to above, you may want to save `.fig` files of the pictures. Again, it allows fine-tuning(In fact, you can extract raw data from .fig files via scripts if you look into it).+As you become a more experienced researcher, you will learn the value of developing a workflow where images can be easily re-generatedThis means that you can easily replot the images when you are finalising the presentation
  
-  - Use [[https://uk.mathworks.com/matlabcentral/fileexchange/23629-export_fig|export_fig]] package (Matlab file exchange) to export to pdf formatIn generaloutput to PDF format unless the image is very detail-heavy (surface plotscontour plotsetc.). This ensures that the image is scalable and crisp+  - **Rule:** Save all data or all scriptsGet in the habit of saving data as ''%%.mat%%'' files.  
 +  **Tip: use scripts to generate your figures.** Create scripts that will load in ''%%.mat%%'' files and plot the data. This means if you ever need to regenerate pictures, you can do it very easilyIf you are performing a numerical experimentmake sure to comment the filename or comment the file directly, indicating important clues about when the figure was generated, and the conditions. 
 +  **Tip: save your figures as .fig.** Similar to aboveyou may want to save ''%%.fig%%'' files of the pictures. Againit allows fine-tuning(In fact, you can extract raw data from ''%%.fig%%'' files via  scripts if you look into it). To save such a file, simply click File > Save-As on the figure window.
  
-Once you have downloaded the `export_figpackage, unzip it and draw the folder into a unified functions folder on your computer. Refer to the [[it_matlab_startup|startup.m]] guide. +===== Exporting figures ===== 
 + 
 +Traditionally (meaning literally in the last 20 years), Matlab has had very poor ability to generate publication-quality images. Problems include: poor bounding boxes, weird rasterisation effects, weird layering issues, etc.  
 + 
 +**Always, always, always** whenever possible, export your images in PDF. This is currently the best format for including into LaTex. When done in the vectorised format of a PDF, the image is then scalable.  
 + 
 +There are exceptions to the above rule in the context of images that are extremely data-hungry (thousands of points) or involve 3D surfaces, contour plots, etc. In those cases, the export to PDF can cause file sizes that are too large, or can cause weird layering effects (due to the complexity of rendering a surface, say). In those cases, you want to export to a rasterised format like PNG.  
 + 
 +Use [[https://uk.mathworks.com/matlabcentral/fileexchange/23629-export_fig|export_fig]] package (Matlab file exchange) to export to pdf format.  
 + 
 +Once you have downloaded the ''%%export_fig%%'' package, unzip it and move the folder into a unified functions folder on your computer. Refer to the [[it_matlab_startup|startup.m]] guide. By doing so, you can issue a command like 
 + 
 +<code matlab> 
 +export_fig 'test.pdf'  
 +</code> 
 + 
 +in order to produce a PDF of the currently focused figure. 
 + 
 +===== Annotations ===== 
 + 
 +In order to add annotations to figures, you have a few options: 
 + 
 +  - Add the annotation natively within Matlab using commands like text(...); this is a good idea in combination with scripts.  
 +  - Add the annotation afterwards by using a vector editor like [[https://en.wikipedia.org/wiki/Inkscape]]. This is not a bad idea and can be quite powerful. However note that it can be difficult due to LaTeX fonts not importing nicely into Inkscape.  
 +  - Use a native LaTeX package like tikz or [[https://www.ctan.org/pkg/pdfpages|pdfpages]] 
 + 
 + 
 +===== Advanced ===== 
 + 
 +More 'advanced' figure generation can involve things like [[https://en.wikipedia.org/wiki/PGF/TikZ|TikZ]], which generates figures natively within LaTeX. The advantage of this approach is consistency in annotation.   
 + 
 +Post-2010 there have been very powerful scripts that allow Matlab figures to be automatically converted to TiKZ, such as [[https://uk.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz|matlab2tikz]]. 
 + 
 +If you are an undergrad student or under pressure to write a report, I don't recommend trying to learn this!