File Exchange

image thumbnail

FIG2MFILE - Save a figure as an M-file (ver 2.0)

version (2.04 KB) by Michael Kleder
Saves a figure as an executable m-file.

1 Download

Updated 31 Oct 2005

No License

FIG2MFILE - save a figure as an executable mfile

USAGE: fig2mfile(fig,'myfig')

fig = figure number of figure to be saved
'myfig' = desired mfile name


(1) Most plots and images can be saved. Most data types can now be handled, including multi-level and multi-dimensional structs and cell arrays. In tests, GUIs can be handled; however, saving a GUI figure window does cannot save its associated callback functions and cannot preserve the numerical value of handles (since they are regenerated at the each figure creation).
(2) To answer the inevitable question, this function will be needed by authors who don't want to include a separate saved figure file in a release of their software, but would rather include the figure generation in their code (e.g. for a compiled software release).
(3) Complex figures can take a few minutes to reduce to m-file format. Disk usage greatly increases from fig-file format as one might expect. (Incidentally, zip-file compression reduces the size to roughly comparable with fig-file size.)
(4) The generated mfile contains a single function ("drawfig") which can be moved into other code as needed.
(5) The created function returns a handle to the figure when it is executed.
(6) ver 1.0: initial writing, Michael Kleder, July 2005
(7) ver 2.0: accept new & multi-level datatypes, Michael Kleder, Oct 2005


title('Squared Peaks')
close all

Cite As

Michael Kleder (2021). FIG2MFILE - Save a figure as an M-file (ver 2.0) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (6)


I cannot get it to work.
Throws the following message and bails out!

Matlab figure data requires 4065932 bytes. (M-file will be larger.)
??? Error using ==> uitools.uimode.modeControl>localModeKeyPressFcn
Too many output arguments.

Error in ==> fig2mfile>show at 100
fprintf(fid,[' ' num2str(s(row,col),18)]);

Error in ==> fig2mfile at 57

The output m file is incomplete and ends abruptly.

Chiaoting Li

j shin

does not work with multiple plots with legend.

The Author

Thank you for asking. The 'Generate M-File' selection under a figure's 'File' menu creates an empty skeleton of the figure containing no actual data, images, annotations, or figure handle. This function actually reproduces the entire figure, returns a handle, and can be invoked automatically (without a mouseclick each time).

Thomas Pieper

Can you explain the difference between your tool and the 'Generate M-File' submenu which can be found in each figure's menu?

The Author

For anyone who is interested, this function works by performing self-recursive function calls down the figure's handle graphics structure, and recording what it finds in text form.

MATLAB Release Compatibility
Created with R14SP1
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!