Code covered by the BSD License  

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

Generate .m-file code for any MATLAB variable

by

 

16 Jun 2009 (Updated )

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

GENERATE CODE FOR VARIABLES

GENERATE CODE FOR VARIABLES

This document demonstrates the usage of GENCODE, GENCODE_RVALUE and GENCODE_SUBSTRUCT to show and modify the contents of complicated MATLAB variables.

Contents

A TEST VARIABLE

The variable x is a struct with two fields: f1 and f2. f1 is a cell array of arbitrary items.

x.f1        = {1 'somestring' false};

f2 is a struct array with 2 members and fields f21, f22, f23. The fields in f2(1) hold different numeric types.

x.f2(1).f21 = diag([1 1 -Inf NaN]);
x.f2(1).f22 = speye(5);
x.f2(1).f23 = 17 * ones(5,6,2,'int8');

The fields in f2(2) hold a cell string array, a logical array and a (anonymous) function handle.

x.f2(2).f21 = {'string 1'; 'string 2'; 'A third string'};
x.f2(2).f22 = rand(5) > .5;
x.f2(2).f23 = @(x)mod(x,2);

GENERATE CODE FOR RIGHT HAND SIDE OF ASSIGNMENTS

GENCODE uses GENCODE_RVALUE to generate code for the right hand side of assignments. This works for

  • all scalar, vector, 2D data
  • cells with scalar, vector, 2D members
  • function handles
[str, sts] = gencode_rvalue(x.f1);
display(sts)
char(str)
sts =

     1


ans =

{           
1           
'somestring'
false       
}'          

x.f2(1).f23 is a 3D array, therefore GENCODE_RVALUE does not work here - sts is false

size(x.f2(1).f23)
[str, sts] = gencode_rvalue(x.f2(1).f23);
display(sts)
char(str)
ans =

     5     6     2


sts =

     0


ans =

     ''


GENERATE CODE FOR TEST VARIABLE

The simplest way to invoke GENCODE is with just the variable as input argument. Code for arrays with more than 2 dimensions is split up into code for 2D subarrays. Code for sparse matrices is generated using three temporary variables tmpi, tmpj and tmps.

strx = gencode(x);
char(strx)
ans =

x.f1 = {                                      
        1                                     
        'somestring'                          
        false                                 
        }';                                   
x.f2(1).f21 = [1 0 0 0                        
               0 1 0 0                        
               0 0 -Inf 0                     
               0 0 0 NaN];                    
tmpi = [1                                     
        2                                     
        3                                     
        4                                     
        5];                                   
tmpj = [1                                     
        2                                     
        3                                     
        4                                     
        5];                                   
tmps = [1                                     
        1                                     
        1                                     
        1                                     
        1];                                   
x.f2(1).f22 = sparse(tmpi, tmpj, tmps);       
x.f2(1).f23(:, :, 1) = int8([17 17 17 17 17 17
                        17 17 17 17 17 17     
                        17 17 17 17 17 17     
                        17 17 17 17 17 17     
                        17 17 17 17 17 17]);  
x.f2(1).f23(:, :, 2) = int8([17 17 17 17 17 17
                        17 17 17 17 17 17     
                        17 17 17 17 17 17     
                        17 17 17 17 17 17     
                        17 17 17 17 17 17]);  
x.f2(2).f21 = {                               
               'string 1'                     
               'string 2'                     
               'A third string'               
               };                             
x.f2(2).f22 = [true false false false true    
               true false true false false    
               false true true true true      
               true true false true true      
               true true true true true];     
x.f2(2).f23 = @(x)mod(x,2);                   

If the name of the variable should be different from the input variable name, GENCODE can be called with an alternative name. This can also be an struct reference (y.a) or cell entry (y{2}) or array index (y(1)).

stry = gencode(x,'y.a');
char(stry)
ans =

y.a.f1 = {                                      
          1                                     
          'somestring'                          
          false                                 
          }';                                   
y.a.f2(1).f21 = [1 0 0 0                        
                 0 1 0 0                        
                 0 0 -Inf 0                     
                 0 0 0 NaN];                    
tmpi = [1                                       
        2                                       
        3                                       
        4                                       
        5];                                     
tmpj = [1                                       
        2                                       
        3                                       
        4                                       
        5];                                     
tmps = [1                                       
        1                                       
        1                                       
        1                                       
        1];                                     
y.a.f2(1).f22 = sparse(tmpi, tmpj, tmps);       
y.a.f2(1).f23(:, :, 1) = int8([17 17 17 17 17 17
                          17 17 17 17 17 17     
                          17 17 17 17 17 17     
                          17 17 17 17 17 17     
                          17 17 17 17 17 17]);  
y.a.f2(1).f23(:, :, 2) = int8([17 17 17 17 17 17
                          17 17 17 17 17 17     
                          17 17 17 17 17 17     
                          17 17 17 17 17 17     
                          17 17 17 17 17 17]);  
y.a.f2(2).f21 = {                               
                 'string 1'                     
                 'string 2'                     
                 'A third string'               
                 };                             
y.a.f2(2).f22 = [true false false false true    
                 true false true false false    
                 false true true true true      
                 true true false true true      
                 true true true true true];     
y.a.f2(2).f23 = @(x)mod(x,2);                   

USE GENERATED CODE TO RECREATE VARIABLE

The generated code can be used to recreate the variable:

clear x
eval(sprintf('%s\n', strx{:}))
display(x)
x = 

    f1: {[1]  'somestring'  [0]}
    f2: [1x2 struct]

Usually, one would write the code to a file using a sequence of commands like this:

fid = fopen('test.m','w');
fprintf(fid, '%s\n', strx{:});
fclose(fid)

This file can then be modified to recreate new instances of x.

Contact us