4.5

4.5 | 2 ratings Rate this file 32 downloads (last 30 days) File Size: 12.99 KB File ID: #24447

Generate .m-file code for any MATLAB variable

by Volkmar Glauche

 

16 Jun 2009 (Updated 23 Jun 2009)

Code covered by BSD License  

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

Download Now | Watch this File

File Information
Description

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
char(str)
% 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)
Zip File Content  
HTML Files GENERATE CODE FOR VARIABLES
Other Files examples/gencode_example.m,
gencode.m,
gencode_rvalue.m,
gencode_substruct.m,
hgsave_pre2008a.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
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

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.

29 Sep 2009 Chamane

helpfull
Mike from http://www.abcourses.fr

Please login to add a comment or rating.
Updates
23 Jun 2009

Fixed bug in sparse matrix creation.
Updated documentation.

Tag Activity for this File
Tag Applied By Date/Time
data export Volkmar Glauche 16 Jun 2009 10:47:27
gui Volkmar Glauche 16 Jun 2009 10:47:27
matrix Volkmar Glauche 16 Jun 2009 10:47:27
data exploration Volkmar Glauche 16 Jun 2009 10:47:27
code Volkmar Glauche 16 Jun 2009 10:47:27
data import Volkmar Glauche 16 Jun 2009 10:47:27
automated program creation Volkmar Glauche 01 Jul 2009 10:50:00
design Volkmar Glauche 01 Jul 2009 10:50:01
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com