zaro

Exporting Image Data from the Workspace

Published in MATLAB Image Export 3 mins read

Exporting images from MATLAB can be done in two primary ways: saving image data stored in a variable or saving a generated figure or plot.

When you have image data stored in a variable in the MATLAB workspace (often as a matrix), you use the imwrite function to save it to a file.

To export data from the MATLAB® workspace using one of the standard graphics file formats, use the imwrite function. This is the standard function for saving raw image data.

Using the imwrite Function

The imwrite function allows you to save an image matrix (A) to a specified file (filename) in a particular format.

  • Syntax: imwrite(A, filename) or imwrite(A, filename, formatSpec)
  • Arguments:
    • A: The image data (a matrix, e.g., uint8, uint16, double).
    • filename: A string specifying the name of the output file (e.g., 'myimage.png'). The file extension often determines the format, but you can also specify it explicitly.
    • formatSpec (optional): A string specifying the file format (e.g., 'png', 'jpeg', 'tiff').

Using this function, you can export data in formats such as Tagged Image File Format (TIFF), Joint Photographic Experts Group (JPEG), and Portable Network Graphics (PNG), as well as many others.

Example:

% Assume 'img' is a variable containing your image data
imwrite(img, 'exported_image.jpg');

% Or explicitly specify the format
imwrite(img, 'exported_image', 'png');

Here is a list of some common image formats supported by imwrite:

  • 'bmp' (Windows Bitmap)
  • 'gif' (Graphics Interchange Format)
  • 'jpg' or 'jpeg' (Joint Photographic Experts Group)
  • 'png' (Portable Network Graphics)
  • 'tif' or 'tiff' (Tagged Image File Format)

Exporting MATLAB Figures and Plots

If you want to save a graph, plot, or any content displayed in a MATLAB figure window as an image file, you typically use the print or saveas functions. These functions capture the visual output of the figure.

Using the print or saveas Function

The print function offers more control over resolution and rendering, making it often preferred for programmatic export. The saveas function is simpler for saving the current figure in various formats.

  • Using print:

    • Syntax: print(filename, options) or print(figure_handle, filename, options)
    • Example:
      % Assume a figure is currently open
      plot(1:10, (1:10).^2);
      print('my_plot.png', '-dpng', '-r300'); % Save as PNG with 300 dpi resolution
    • Common format options start with -d (e.g., -dpng, -djpeg, -dtiff, -dsvg for scalable vector graphics, -depsc for encapsulated PostScript).
  • Using saveas:

    • Syntax: saveas(figure_handle, filename, format)
    • Example:
      % Assume a figure is currently open
      figure; % Create a new figure
      imshow('peppers.png');
      saveas(gcf, 'peppers_figure.tiff'); % Save the current figure as TIFF
    • Common format options are strings like 'png', 'jpeg', 'tiff', 'svg', 'eps'.

Summary of Export Methods

Here's a quick overview of the two main approaches:

Method Purpose Key Function(s) Typical Input Typical Output
Exporting Image Data Save image matrices from workspace imwrite Image data (matrix) Image file (e.g., JPG, PNG)
Exporting Figures/Plots Save generated plots or figure windows print, saveas Figure handle or gcf Image/Vector file (e.g., PNG, JPG, SVG)

Choose the method that best suits whether you are trying to save raw image data or the visual content of a figure window.