No BSD License  

Highlights from
serialize

5.0

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

serialize

by Jøger Hansegård

 

26 Aug 2006 (Updated 29 Aug 2006)

Create matlab code from a variable

| Watch this File

File Information
Description

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
  eval(matcode)

Examples
  x = [1 2 3; 3 4 5];
  serialize(x)
  
  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)

Acknowledgements
This submission has inspired the following:
Serialize/Deserialize
MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
28 Aug 2006 Steve Orzol

x{1} = 'rick''s'

y = serialize(x)

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

29 Aug 2006 Jøger Hansegård

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

29 Aug 2006 Steve Orzol

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

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.

09 Nov 2006 Tom Muylle

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

03 Mar 2010 Matt Dunham

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

05 Mar 2010 Matt Dunham

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));
end

Please login to add a comment or rating.
Updates
29 Aug 2006

Fixed bug when serializing strings with special characters

Tag Activity for this File
Tag Applied By Date/Time
data import Jøger Hansegård 22 Oct 2008 08:35:53
data export Jøger Hansegård 22 Oct 2008 08:35:53
mat2str num2str int2str sprintf class eval Jøger Hansegård 22 Oct 2008 08:35:53
data export Laszlo Balazs 15 Nov 2010 07:46:08

Contact us at files@mathworks.com