No License

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

» Watch video

Highlights from

5.0 | 3 ratings Rate this file 5 Downloads (last 30 days) File Size: 5.02 KB File ID: #12063 Version: 1.0




26 Aug 2006 (Updated )

Create matlab code from a variable

| Watch this File

File Information

matcode = SERIALIZE(x) generates matlab code of x
matcode = SERIALIZE(x, n) generates matlab code of x retaining n digits
of precision

SERIALIZE should be able to create matlab code of the following data types:
- matrices, vectors, and scalars of any class and dimension
- strings
- structs, arrays of structs with up to six dimensions
- cell arrays
- matlab objects with a copy constructor implemented
- empty values of any class
- any combinations hereof

The value of x can be obtained by

  x = [1 2 3; 3 4 5];
  x = uint8(rand(10)*5);
  matcode = serialize(x)

  x = {rand(3,3,3,4), 'a string value', {1 2 3; 1 3 3 }}
  matcode = serialize(x, 30)


This file inspired Serialize/Deserialize.

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
24 Jul 2014 Giuliano Vecchiarelli

As Sebastian already said, empty structures need to be fixed.

I suggest you to substitute the code in line 134:
val = 'struct(';

with the following one:
if numel(fieldNames) == 0
val = 'struct';
val = 'struct(';

Comment only
12 Jun 2014 Sebastian

Empty structures are not handled correctly:
>> serialize(struct)
ans = 'struc);'

Comment only
05 Mar 2010 Matt Dunham

Matt Dunham (view profile)

It would be nice if it also serialized function handles - the following changes would do the trick.

elseif isa(v, 'function_handle')
val = serializehandle(v);

function val = serializehandle(v)
val = sprintf('str2func(''%s'')', func2str(v));

Comment only
03 Mar 2010 Matt Dunham

Matt Dunham (view profile)

This is fantastic - exactly what I was looking for. Thanks

09 Nov 2006 Tom Muylle

It's too bad it doesn't work for matlab 6.5 :(

Comment only
09 Sep 2006 anony mouse

An absolutely wonderful piece of code. I wish I now had the ability to read the output into software like excel.

29 Aug 2006 Steve Orzol

Nice. Thanks for the quick update. I can see using this with building xml strings.

29 Aug 2006 Jøger Hansegård

This has been fixed by converting such strings to uint8. Thanks for noticing me.

Comment only
28 Aug 2006 Steve Orzol

x{1} = 'rick''s'

y = serialize(x)

z = eval(y) %gives ??? Error: Unexpected MATLAB expression.

Comment only

Contact us