View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Generate .m-file code for any MATLAB variable

4.9 | 7 ratings Rate this file 17 Downloads (last 30 days) File Size: 13 KB File ID: #24447 Version: 1.1

Generate .m-file code for any MATLAB variable



16 Jun 2009 (Updated )

Generate .m script code for any MATLAB variable. If run, the code recreates the input variable.

| Watch this File

File Information

Have you ever tried to inspect and modify deeply nested struct/cell variables? Of course, MATLABs variable editor will help to visualise these variables. However, to create such a variable automatically or to change some of the deeply nested fields routinely this submission may be useful.
The main function is GENCODE. This function traverses the input variable recursively. Recursion stops if the value of the current part of the input variable can be expressed as a right hand side of a MATLAB assignment (usually, this is the case for up to 2D matrices, character or cell arrays). In this case, GENCODE_RVALUE is called to create code for the right hand side of the assignment, and GENCODE_SUBSTRUCT is called to create the correct reference into the input variable.

A very simple example:
% struct variable with 2 fields 'a' and 'b'
x.a = 1;
x.b = 'somestring';
% generate code for it
str = gencode(x);
% display this code
% clear original variable
clear x
% recreate it by evaluating the code
eval(sprintf('%s', str{:}))

A more realistic example is provided in hgsave_pre2008a.m. In MATLAB 2008a, GUIDE saves %automatic uicontrol callbacks as anonymous function handles. Unfortunately, MATLAB R14SP3 crashes when loading .fig/.mat files that contain certain anonymous functions. One possible workaround is to
1. open the .fig file in MATLAB 2008a
2. generate code for it
3. run the generated code in MATLAB R14SP3
4. save the resulting variable in a new .fig file

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
19 Jan 2017 priti sharma

gencode() function is not working,

displaying error msg:
??? Undefined function or method 'gencode' for input
arguments of type 'struct'.

Comment only
02 Oct 2016 gooey

gooey (view profile)

17 Aug 2016 Mark Watson

Great, really useful.

In 2016a I found that textscan no longer supports bufsize, so this needs patching.

In gencode_rvalue.m line 119
if verLessThan('matlab', '8.6')
str1 = textscan(sitem, '%s', 'delimiter',';', 'bufsize',bsz);
str1 = textscan(sitem, '%s', 'delimiter',';');

23 Jun 2016 Xenya

Xenya (view profile)

Useful thing, but seems cell array is getting transposed:

z= { 1 2 3 }'

z =


z =

[1] [2] [3]

Comment only
23 Jun 2016 Xenya

Xenya (view profile)

27 May 2014 Balazs

Balazs (view profile)

Great piece of code, works perfectly.

29 Aug 2011 Cody

Cody (view profile)

This saved me a lot of hassle and worked perfectly. Thanks!

29 Sep 2009 Chamane

Mike from

07 Jul 2009 Volkmar Glauche

I decided not to have this compact mode because lines may get really long. But I will think about it again.

Comment only
03 Jul 2009 Gabriel Akira Schreiber

This is exactly what I was looking for, thank you very much.

One suggestion remains though:
1: A 'compact' mode without unnecessary spaces to conserve space

23 Jun 2009 1.1

Fixed bug in sparse matrix creation.
Updated documentation.

Contact us