No BSD License  

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

2.33333

2.3 | 3 ratings Rate this file 2 Downloads (last 30 days) File Size: 5.49 KB File ID: #8000
image thumbnail

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

by Michael Kleder

 

08 Jul 2005 (Updated 31 Oct 2005)

Saves a figure as an executable m-file.

| Watch this File

File Information
Description

FIG2MFILE - save a figure as an executable mfile

USAGE: fig2mfile(fig,'myfig')

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

NOTES:

(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

EXAMPLE:

surf(peaks+peaks.^2)
title('Squared Peaks')
fig2mfile(gcf,'mypeaks')
close all
h=mypeaks

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
12 Jul 2005 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.

13 Jul 2005 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?

13 Jul 2005 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).

12 Sep 2007 j shin

does not work with multiple plots with legend.

27 Nov 2007 Chiaoting Li  
22 Apr 2009 Bhargava

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

fig2mfile(gcf,'savefig_trial')
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
    show(eval(q{n}),q{n},fid)

The output m file is incomplete and ends abruptly.

Please login to add a comment or rating.
Updates
14 Jul 2005

Function definition line had been inadvertently commented out.

31 Oct 2005

Handles much more complicated figures including GUIs and figures containing multi-level and multi-dimensional structs and cell arrays.

Tag Activity for this File
Tag Applied By Date/Time
specialized Michael Kleder 22 Oct 2008 07:52:31
plotting Michael Kleder 22 Oct 2008 07:52:31
save figure mfile mfile hgsave Michael Kleder 22 Oct 2008 07:52:31

Contact us at files@mathworks.com